

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>scitools.easyviz &mdash; SciTools 0.8.3 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.8.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="SciTools 0.8.3 documentation" href="index.html" />
    <link rel="next" title="scitools.easyviz.blt_" href="blt_.html" />
    <link rel="prev" title="scitools.std" href="std.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="blt_.html" title="scitools.easyviz.blt_"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="std.html" title="scitools.std"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">SciTools 0.8.3 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-scitools.easyviz">
<span id="scitools-easyviz"></span><h1><a class="reference internal" href="#module-scitools.easyviz" title="scitools.easyviz"><tt class="xref py py-mod docutils literal"><span class="pre">scitools.easyviz</span></tt></a><a class="headerlink" href="#module-scitools.easyviz" title="Permalink to this headline">¶</a></h1>
<div class="section" id="easyviz">
<h2>Easyviz<a class="headerlink" href="#easyviz" title="Permalink to this headline">¶</a></h2>
<p>Easyviz is a unified interface to various packages for scientific
visualization and plotting.  The Easyviz interface is written in
Python with the purpose of making it very easy to visualize data in
Python scripts. Both curve plots and more advanced 2D/3D visualization
of scalar and vector fields are supported.  The Easyviz interface was
designed with three ideas in mind: 1) a simple, Matlab-like syntax; 2)
a unified interface to lots of visualization engines (called backends
later): Gnuplot, Matplotlib, Grace, Veusz, Pmw.Blt.Graph, PyX,
Matlab, VTK, VisIt, OpenDX; and 3) a minimalistic interface which
offers only basic control of plots: curves, linestyles, legends,
title, axis extent and names.  More fine-tuning of plots can be done
by invoking backend-specific commands.</p>
<p>Easyviz was made so that one can postpone the choice of a particular
visualization package (and its special associated syntax). This is
often useful when you quickly need to visualize curves or 2D/3D fields
in your Python program, but haven&#8217;t really decided which plotting tool
to go for. As Python is gaining popularity at universities, students
are often forced to continuously switch between Matlab and Python,
which is straightforward for array computing, but (previously)
annoying for plotting. Easyviz was therefore also made to ease the
switch between Python and Matlab.</p>
<p>If you encounter problems with using Easyviz, please visit the 
<em>Troubleshooting</em> chapter and the <em>Installation</em> chapter at the
end of the documentation.</p>
<div class="section" id="easyviz-documentation">
<h3>Easyviz Documentation<a class="headerlink" href="#easyviz-documentation" title="Permalink to this headline">¶</a></h3>
<p>The present documentation is available in a number of formats:</p>
<blockquote>
<div><ul class="simple">
<li>PDF (<a class="reference external" href="https://scitools.googlecode.com/hg/doc/easyviz/easyviz.pdf">https://scitools.googlecode.com/hg/doc/easyviz/easyviz.pdf</a>)</li>
<li>Plain HTML (<a class="reference external" href="https://scitools.googlecode.com/hg/doc/easyviz/easyviz.html">https://scitools.googlecode.com/hg/doc/easyviz/easyviz.html</a>)</li>
<li>Sphinx HTML (<a class="reference external" href="https://scitools.googlecode.com/hg/doc/easyviz/easyviz_sphinx_html/index.html">https://scitools.googlecode.com/hg/doc/easyviz/easyviz_sphinx_html/index.html</a>)</li>
<li>Plain text (<a class="reference external" href="https://scitools.googlecode.com/hg/doc/easyviz/easyviz.txt">https://scitools.googlecode.com/hg/doc/easyviz/easyviz.txt</a>)</li>
<li>Wiki (<a class="reference external" href="http://code.google.com/p/scitools/wiki/EasyvizDocumentation">http://code.google.com/p/scitools/wiki/EasyvizDocumentation</a>)</li>
<li>Doconce source (<a class="reference external" href="https://scitools.googlecode.com/hg/doc/easyviz/easyviz.do.txt">https://scitools.googlecode.com/hg/doc/easyviz/easyviz.do.txt</a>)</li>
</ul>
</div></blockquote>
<p>The documentation is written in the 
Doconce (<a class="reference external" href="http://code.google.com/p/doconce">http://code.google.com/p/doconce</a>) 
format and can be translated into a
number of different formats (reST, Sphinx, LaTeX, HTML, XML,
OpenOffice, RTF, Word, and plain untagged ASCII).</p>
</div>
<div class="section" id="guiding-principles">
<h3>Guiding Principles<a class="headerlink" href="#guiding-principles" title="Permalink to this headline">¶</a></h3>
<p><em>First principle.</em> Array data can be plotted with a minimal
set of keystrokes using a Matlab-like syntax. A simple:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">t</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">51</span><span class="p">)</span>    <span class="c"># 51 points between 0 and 3</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> 
</pre></div>
</div>
<p>plots the data in (the NumPy array) t versus the data in (the NumPy
array) y. If you need legends, control of the axis, as well as
additional curves, all this is obtained by the standard Matlab-style
commands:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">y2</span> <span class="o">=</span> <span class="n">t</span><span class="o">**</span><span class="mi">4</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="c"># pick out each 4 points and add random noise:</span>
<span class="n">t3</span> <span class="o">=</span> <span class="n">t</span><span class="p">[::</span><span class="mi">4</span><span class="p">]</span>
<span class="n">y3</span> <span class="o">=</span> <span class="n">y2</span><span class="p">[::</span><span class="mi">4</span><span class="p">]</span> <span class="o">+</span> <span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">0.02</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">t3</span><span class="p">))</span>

<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="s">&#39;r-&#39;</span><span class="p">)</span>
<span class="n">hold</span><span class="p">(</span><span class="s">&#39;on&#39;</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y2</span><span class="p">,</span> <span class="s">&#39;b-&#39;</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t3</span><span class="p">,</span> <span class="n">y3</span><span class="p">,</span> <span class="s">&#39;bo&#39;</span><span class="p">)</span>
<span class="n">legend</span><span class="p">(</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;t^4*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;data&#39;</span><span class="p">)</span>
<span class="n">title</span><span class="p">(</span><span class="s">&#39;Simple Plot Demo&#39;</span><span class="p">)</span>
<span class="n">axis</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.05</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">])</span>
<span class="n">xlabel</span><span class="p">(</span><span class="s">&#39;t&#39;</span><span class="p">)</span>
<span class="n">ylabel</span><span class="p">(</span><span class="s">&#39;y&#39;</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>

<span class="n">hardcopy</span><span class="p">(</span><span class="s">&#39;tmp0.eps&#39;</span><span class="p">)</span>  <span class="c"># this one can be included in LaTeX</span>
<span class="n">hardcopy</span><span class="p">(</span><span class="s">&#39;tmp0.png&#39;</span><span class="p">)</span>  <span class="c"># this one can be included in HTML</span>
</pre></div>
</div>
<p>Easyviz also allows these additional function calls to be executed
as a part of the plot call:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="s">&#39;r-&#39;</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">y2</span><span class="p">,</span> <span class="s">&#39;b-&#39;</span><span class="p">,</span> <span class="n">t3</span><span class="p">,</span> <span class="n">y3</span><span class="p">,</span> <span class="s">&#39;bo&#39;</span><span class="p">,</span>
     <span class="n">legend</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;t^4*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;data&#39;</span><span class="p">),</span>
     <span class="n">title</span><span class="o">=</span><span class="s">&#39;Simple Plot Demo&#39;</span><span class="p">,</span>
     <span class="n">axis</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.05</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">),</span>
     <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;y&#39;</span><span class="p">,</span>
     <span class="n">hardcopy</span><span class="o">=</span><span class="s">&#39;tmp1.eps&#39;</span><span class="p">,</span>
     <span class="n">show</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="n">hardcopy</span><span class="p">(</span><span class="s">&#39;tmp0.png&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>A scalar function f(x,y) may be visualized
as an elevated surface with colors using these commands:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">41</span><span class="p">)</span>  <span class="c"># 41 point on [-2, 2]</span>
<span class="n">xv</span><span class="p">,</span> <span class="n">yv</span> <span class="o">=</span> <span class="n">ndgrid</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>    <span class="c"># define a 2D grid with points (xv,yv)</span>
<span class="n">values</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">)</span>       <span class="c"># function values</span>
<span class="n">surfc</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span>
      <span class="n">shading</span><span class="o">=</span><span class="s">&#39;interp&#39;</span><span class="p">,</span>
      <span class="n">clevels</span><span class="o">=</span><span class="mi">15</span><span class="p">,</span>
      <span class="n">clabels</span><span class="o">=</span><span class="s">&#39;on&#39;</span><span class="p">,</span>
      <span class="n">hidden</span><span class="o">=</span><span class="s">&#39;on&#39;</span><span class="p">,</span>
      <span class="n">show</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p><em>Second princple.</em> Easyviz is just a unified interface to other
plotting packages that can be called from Python. Such plotting
packages are referred to as backends. Several backends are supported:
Gnuplot, Matplotlib, Grace (Xmgr), Veusz, Pmw.Blt.Graph, PyX, Matlab,
VTK, VisIt, OpenDX. In other words, scripts that use Easyviz commands
only, can work with a variety of backends, depending on what you have
installed on the machine in question and what quality of the plots you
demand. For example, switching from Gnuplot to Matplotlib is trivial.</p>
<p>Scripts with Easyviz commands will most probably run anywhere since at
least the Gnuplot package can always be installed right away on any
platform. In practice this means that when you write a script to
automate investigation of a scientific problem, you can always quickly
plot your data with Easyviz (i.e., Matlab-like) commands and postpone
to marry any specific plotting tool. Most likely, the choice of
plotting backend can remain flexible. This will also allow old scripts
to work with new fancy plotting packages in the future if Easyviz
backends are written for those packages.</p>
<p><em>Third principle.</em> The Easyviz interface is minimalistic, aimed at
rapid prototyping of plots. This makes the Easyviz code easy to read
and extend (e.g., with new backends). If you need more sophisticated
plotting, like controlling tickmarks, inserting annotations, etc., you
must grab the backend object and use the backend-specific syntax to
fine-tune the plot. The idea is that you can get away with Easyviz and
a plotting package-independent script &#8220;95 percent&#8221; of the time - only
now and then there will be demand for package-dependent code for
fine-tuning and customization of figures.</p>
<p>These three principles and the Easyviz implementation make simple things
simple and unified, and complicated things are not more complicated than
they would otherwise be. You can always start out with the simple
commands - and jump to complicated fine-tuning only when strictly needed.</p>
</div>
</div>
<div class="section" id="tutorial">
<h2>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h2>
<p>This tutorial starts with plotting a single curve with a simple
plot(x,y) command. Then we add a legend, axis labels, a title, etc.
Thereafter we show how multiple curves are plotted together. We also
explain how line styles and axis range can be controlled. The
next topic deals with animations and making movie files. More advanced
subjects, such as fine tuning of plots (using plotting package-specific
commands) and working with Axis and Figure objects, close the curve
plotting part of the tutorial.</p>
<p>Various methods for visualization of scalar fields in 2D and 3D are
treated next, before we show how 2D and 3D vector fields can be handled.</p>
<div class="section" id="a-note-on-import-statements">
<h3>A Note on Import Statements<a class="headerlink" href="#a-note-on-import-statements" title="Permalink to this headline">¶</a></h3>
<p>The recommended standard import of numpy
and matplotlib in programs reads:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
</pre></div>
</div>
<p>This import ensures that all functionality from different packages are
prefixed by a short form of the package name. This convention has,
from a computer science perspective, many advantages as one sees
clearly where functionality comes from.  However, convincing
scientists with extensive Matlab, Fortran, or C++ experience to switch
to Python can be hard when mathematical formulas are full of np.
prefixes and all plotting commands are decorated with an &#8220;extra&#8221;
plt. The developers of Easyviz think it is a major point to have
Python code as close to Matlab and standard mathematical syntax as
possible.  Therefore, examples in this manual employ the &#8220;star
import&#8221;:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>This statement imports the Easyviz plotting commands and also performs
from numpy import <a href="#id1"><span class="problematic" id="id2">*</span></a>. Hence, mathematical functions like sin and
log are available and work for arrays, as in Matlab, and the plotting
commands are the same as those in Matlab. This type of import statement
is similar to the popular:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">matplotlib.pylab</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>among Matplotlib users (although not promoted by Matplotlib developers). 
The primary additional feature of the
scitools.std import is the possibility to choose among many different
backends for plotting, where Matplotlib is one of the options.</p>
</div>
<div class="section" id="plotting-a-single-curve">
<h3>Plotting a Single Curve<a class="headerlink" href="#plotting-a-single-curve" title="Permalink to this headline">¶</a></h3>
<p>Let us plot the curve y = t**2*exp(-t**2) for
t values between 0 and 3.  First we generate equally spaced
coordinates for t, say 51 values (50 intervals). Then we compute the
corresponding y values at these points, before we call the
plot(t,y) command to make the curve plot.  Here is the complete
program:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>

<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>

<span class="n">t</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">51</span><span class="p">)</span>    <span class="c"># 51 points between 0 and 3</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>         <span class="c"># allocate y with float elements</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">t</span><span class="p">)):</span>
    <span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>

<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>  <span class="c"># optional</span>
</pre></div>
</div>
<p>The first line imports all of SciTools and Easyviz that can be handy
to have when doing scientific computations. This includes everything
from numpy (from numpy import <a href="#id3"><span class="problematic" id="id4">*</span></a>), 
all Easyviz plotting commands, some modules (sys, math), and
all of SciPy (from scipy import <a href="#id5"><span class="problematic" id="id6">*</span></a>) if SciPy is installed.
In the program above, we first
pre-allocate the y array and fill it with values, element by
element, in a Python loop. Alternatively, we may operate
on the whole t array at once, which yields faster and shorter code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>

<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>

<span class="n">t</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">51</span><span class="p">)</span>    <span class="c"># 51 points between 0 and 3</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>                  <span class="c"># compute all f values at once</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>                    <span class="c"># optional</span>
</pre></div>
</div>
<p>The f function can also be skipped, if desired, so that we can write
directly:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">y</span> <span class="o">=</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>To include the plot in electronic documents, we need a hardcopy of the
figure in PostScript, PNG, or another image format.  The hardcopy
command produces files with images in various formats:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">hardcopy</span><span class="p">(</span><span class="s">&#39;tmp1.eps&#39;</span><span class="p">)</span> <span class="c"># produce PostScript</span>
<span class="n">hardcopy</span><span class="p">(</span><span class="s">&#39;tmp1.png&#39;</span><span class="p">)</span> <span class="c"># produce PNG</span>
</pre></div>
</div>
<p>An alternative name for hardcopy is savefig:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">savefig</span><span class="p">(</span><span class="s">&#39;tmp1.eps&#39;</span><span class="p">)</span> <span class="c"># produce PostScript</span>
<span class="n">savefig</span><span class="p">(</span><span class="s">&#39;tmp1.png&#39;</span><span class="p">)</span> <span class="c"># produce PNG</span>
</pre></div>
</div>
<p>The filename extension determines the format: .ps or
.eps for PostScript, and .png for PNG. 
Figure ref{fig:plot1a} displays the resulting plot. With show(False)
we can suppress the plot from being shown at the screen, which is
useful when create a large number of figure files in programs.</p>
<p>FIGURE:[figs/plot1a.eps] A simple plot in PostScript format. {fig:plot1a}</p>
<p>On some platforms, some backends may result in a plot that is shown in
just a fraction of a second on the screen before the plot window disappears
(using the Gnuplot backend on Windows machines or using the Matplotlib
backend constitute two examples). To make the window stay on the screen,
add:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nb">raw_input</span><span class="p">(</span><span class="s">&#39;Press the Return key to quit: &#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>at the end of the program. The plot window is killed when the program
terminates, and this satement postpones the termination until the user
hits the Return key.</p>
</div>
<div class="section" id="decorating-the-plot">
<h3>Decorating the Plot<a class="headerlink" href="#decorating-the-plot" title="Permalink to this headline">¶</a></h3>
<p>The x and y axes in curve plots should have labels, here t and
y, respectively. Also, the curve should be identified with a label,
or legend as it is often called.  A title above the plot is also
common.  In addition, we may want to control the extent of the axes (although
most plotting programs will automatically adjust the axes to the range of the
data).
All such things are easily added after the plot command:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">xlabel</span><span class="p">(</span><span class="s">&#39;t&#39;</span><span class="p">)</span>
<span class="n">ylabel</span><span class="p">(</span><span class="s">&#39;y&#39;</span><span class="p">)</span>
<span class="n">legend</span><span class="p">(</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">)</span>
<span class="n">axis</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.05</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">])</span>   <span class="c"># [tmin, tmax, ymin, ymax]</span>
<span class="n">title</span><span class="p">(</span><span class="s">&#39;My First Easyviz Demo&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>This syntax is inspired by Matlab to make the switch between
Easyviz and Matlab almost trivial.
Easyviz has also introduced a more &#8220;Pythonic&#8221; plot command where
all the plot properties can be set at once:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span>
     <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span>
     <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;y&#39;</span><span class="p">,</span>
     <span class="n">legend</span><span class="o">=</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">,</span>
     <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.05</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">],</span>
     <span class="n">title</span><span class="o">=</span><span class="s">&#39;My First Easyviz Demo&#39;</span><span class="p">,</span>
     <span class="n">savefig</span><span class="o">=</span><span class="s">&#39;tmp1.eps&#39;</span><span class="p">,</span>  <span class="c"># or hardcopy=&#39;tmp1.eps&#39;</span>
     <span class="n">show</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>With show=False one can avoid the plot window on the screen and
just make the hardcopy. This feature is particularly useful if
one generates a large number of separate figures in the program.
The keyword savefig can be replaced by hardcopy if desired.</p>
<p>Note that we in the curve legend write t square as t^2 (LaTeX style)
rather than t**2 (program style). Whichever form you choose is up to
you, but the LaTeX form sometimes looks better in some plotting
programs (Matplotlib and Gnuplot are two examples). 
See Figure ref{fig:plot1c} for what the modified
plot looks like and how t^2 is typeset in Gnuplot.</p>
<p>FIGURE:[figs/plot1c.eps] A single curve with label, title, and axes adjusted. {fig:plot1c}</p>
</div>
<div class="section" id="plotting-multiple-curves">
<h3>Plotting Multiple Curves<a class="headerlink" href="#plotting-multiple-curves" title="Permalink to this headline">¶</a></h3>
<p>A common plotting task is to compare two or more curves, which
requires multiple curves to be drawn in the same plot.
Suppose we want to plot the two functions f_1(t)=t^2exp(-t^2)
and f_2(t)=t^4exp(-t^2). If we write two plot commands after
each other, two separate plots will be made. To make the second
plot command draw the curve in the first plot, we need to
issue a hold(&#8216;on&#8217;) command. Alternatively, we can provide all
data in a single plot command. A complete program illustrates the
different approaches:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>   <span class="c"># for curve plotting</span>

<span class="k">def</span> <span class="nf">f1</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">f2</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">f1</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>

<span class="n">t</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">51</span><span class="p">)</span>
<span class="n">y1</span> <span class="o">=</span> <span class="n">f1</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="n">y2</span> <span class="o">=</span> <span class="n">f2</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>

<span class="c"># Matlab-style syntax</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">)</span>
<span class="n">hold</span><span class="p">(</span><span class="s">&#39;on&#39;</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y2</span><span class="p">)</span>

<span class="n">xlabel</span><span class="p">(</span><span class="s">&#39;t&#39;</span><span class="p">)</span>
<span class="n">ylabel</span><span class="p">(</span><span class="s">&#39;y&#39;</span><span class="p">)</span>
<span class="n">legend</span><span class="p">(</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;t^4*exp(-t^2)&#39;</span><span class="p">)</span>
<span class="n">title</span><span class="p">(</span><span class="s">&#39;Plotting two curves in the same plot&#39;</span><span class="p">)</span>
<span class="n">savefig</span><span class="p">(</span><span class="s">&#39;tmp2.eps&#39;</span><span class="p">)</span>  <span class="c"># or hardcopy(&#39;tmp2.eps&#39;)</span>

<span class="c"># Alternative &quot;Pythonic&quot; style</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">y2</span><span class="p">,</span> <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;y&#39;</span><span class="p">,</span>
     <span class="n">legend</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;t^4*exp(-t^2)&#39;</span><span class="p">),</span>
     <span class="n">title</span><span class="o">=</span><span class="s">&#39;Plotting two curves in the same plot&#39;</span><span class="p">,</span>
     <span class="n">savefig</span><span class="o">=</span><span class="s">&#39;tmp2.eps&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The sequence of the multiple legends is such that the first legend 
corresponds to the first curve, the second legend to the second curve,
and so on. The visual result appears in Figure ref{fig:plot2a}.</p>
<p>Doing a hold(&#8216;off&#8217;) makes the next plot command create a new
plot in the same window. This new plot just erases the previous curves.</p>
<p>FIGURE:[figs/plot2a.eps] Two curves in the same plot. {fig:plot2a}</p>
<p>With the keyword argrument grid=True to plot we can add a
grid, which is frequently used when plotting curves (see
Figure ref{fig:plot2f}).</p>
<p>FIGURE:[figs/plot2f.eps] Curves with a grid. {fig:plot2f}</p>
<p>The default location of the legends is dependent on the backend
(some have a fixed location, like Gnuplot, and some try to find
the most optimal location, like Matplotlib). One can control
the location by the loc keyword to the legend function, e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">legend</span><span class="p">(</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;t^4*exp(-t^2)&#39;</span><span class="p">,</span> <span class="n">loc</span><span class="o">=</span><span class="s">&#39;upper left&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The most popular values are upper right, upper left, lower left, 
and lower right, depending on the shape of the curves and extend
of the axes. The keyword argument fancybox draws a box around
the legends if True, otherwise no box is drawn. The corresponding
keywords for the plot function are legend_loc and legend_fancybox:</p>
<div class="highlight-python"><pre>plot(t, y1, t, y2, xlabel='t', ylabel='y',
     legend=('t^2*exp(-t^2)', 't^4*exp(-t^2)'),
     legend_loc=`upper left`, legend_fancybox=True,
     axis=[0, 4, -0.1, 0.8],
     title='Plotting two curves in the same plot',
     savefig='tmp2.eps')</pre>
</div>
<p>The loc and fancybox specifications work (at present)
with Gnuplot and Matplotlib only.</p>
<p>FIGURE: [figs/plot2l.eps, width=400] A figure with legends placed to the upper left with a box frame.</p>
<p>The legend function also accepts a list of legends instead of
the legends as separate positional arguments. This allows an overlapping
syntax between Matplotlib and Easyviz so that the same code can apply
either of the packages (however, Matplotlib&#8217;s keywords to 
plot, like label and linewidth, are not recognized so not all
syntax is interchangable).</p>
</div>
<div class="section" id="making-multiple-figures">
<h3>Making Multiple Figures<a class="headerlink" href="#making-multiple-figures" title="Permalink to this headline">¶</a></h3>
<p>The hold command either adds a new curve or replaces old curve(s) by
new ones. Often one wants to make multiple figures in a program,
realized as multiple windows on the screen. The figure() command
creates a new figure:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">81</span><span class="p">)</span>
<span class="n">y1</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">pi</span><span class="o">*</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y1</span><span class="p">)</span>

<span class="n">figure</span><span class="p">()</span>  <span class="c"># separate plot window</span>
<span class="n">y2</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">pi</span><span class="o">*</span><span class="n">x</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y2</span><span class="p">)</span>

<span class="n">figure</span><span class="p">()</span>  <span class="c"># yet another plot window</span>
<span class="n">y3</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">pi</span><span class="o">*</span><span class="n">x</span><span class="o">/</span><span class="mi">4</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y3</span><span class="p">)</span>
</pre></div>
</div>
<p>More information in the figure command is found later on under the
heading <em>Working with Axis and Figure Objects</em>.</p>
</div>
<div class="section" id="controlling-line-styles">
<h3>Controlling Line Styles<a class="headerlink" href="#controlling-line-styles" title="Permalink to this headline">¶</a></h3>
<p>When plotting multiple curves in the same plot, the individual curves
get distinct default line styles, depending on the program that is
used to produce the curve (and the settings for this program). It
might well happen that you get a green and a red curve (which is bad
for a significant portion of the male population).  Therefore,
we often want to control the line style in detail. Say we want the first
curve (t and y1) to be drawn as a red solid line and the second
curve (t and y2) as blue circles at the discrete data points.  The
Matlab-inspired syntax for specifying line types applies a letter for
the color and a symbol from the keyboard for the line type. For
example, r- represents a red (r) line (-), while bo means blue
(b) circles (o). The line style specification is added as an
argument after the x and y coordinate arrays of the curve:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="s">&#39;r-&#39;</span><span class="p">)</span>
<span class="n">hold</span><span class="p">(</span><span class="s">&#39;on&#39;</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y2</span><span class="p">,</span> <span class="s">&#39;bo&#39;</span><span class="p">)</span>

<span class="c"># or</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="s">&#39;r-&#39;</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">y2</span><span class="p">,</span> <span class="s">&#39;bo&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The effect of controlling the line styles can be seen in 
Figure ref{fig:plot2c}.</p>
<p>FIGURE:[figs/plot2c.eps] Two curves in the same plot, with controlled line styles. {fig:plot2c}</p>
<p>Assume now that we want to plot the blue circles at every 4 points only.
We can grab every 4 points out of the t array by using an appropriate
slice: t2 = t[::4]. Note that the first colon means the range from the
first to the last data point, while the second colon separates this
range from the stride, i.e., how many points we should &#8220;jump over&#8221;
when we pick out a set of values of the array:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>

<span class="k">def</span> <span class="nf">f1</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">f2</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">f1</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>

<span class="n">t</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">51</span><span class="p">)</span>
<span class="n">y1</span> <span class="o">=</span> <span class="n">f1</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="n">t2</span> <span class="o">=</span> <span class="n">t</span><span class="p">[::</span><span class="mi">4</span><span class="p">]</span>
<span class="n">y2</span> <span class="o">=</span> <span class="n">f2</span><span class="p">(</span><span class="n">t2</span><span class="p">)</span>

<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="s">&#39;r-6&#39;</span><span class="p">,</span> <span class="n">t2</span><span class="p">,</span> <span class="n">y2</span><span class="p">,</span> <span class="s">&#39;bo3&#39;</span><span class="p">,</span>
     <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;y&#39;</span><span class="p">,</span>
     <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">],</span>
     <span class="n">legend</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;t^4*exp(-t^2)&#39;</span><span class="p">),</span>
     <span class="n">title</span><span class="o">=</span><span class="s">&#39;Plotting two curves in the same plot&#39;</span><span class="p">,</span>
     <span class="n">hardcopy</span><span class="o">=</span><span class="s">&#39;tmp2.eps&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>In this plot we also adjust the size of the line and the circles by
adding an integer: r-6 means a red line with thickness 6 and bo5
means red circles with size 5. The effect of the given line thickness
and symbol size depends on the underlying plotting program. For
the Gnuplot program one can view the effect in Figure ref{fig:plot2g}.</p>
<p>FIGURE:[figs/plot2g.eps] Circles at every 4 points and extended line thickness (6) and circle size (3). {fig:plot2g}</p>
<dl class="docutils">
<dt>The different available line colors include </dt>
<dd><ul class="first last simple">
<li>yellow:   &#8216;y&#8217;</li>
<li>magenta:  &#8216;m&#8217;</li>
<li>cyan:     &#8216;c&#8217;</li>
<li>red:      &#8216;r&#8217;</li>
<li>green:    &#8216;g&#8217;</li>
<li>blue:     &#8216;b&#8217;</li>
<li>white:    &#8216;w&#8217;</li>
<li>black:    &#8216;k&#8217;</li>
</ul>
</dd>
<dt>The different available line types are</dt>
<dd><ul class="first last simple">
<li>solid line:      &#8216;-&#8216;</li>
<li>dashed line:     &#8216;&#8211;&#8217;</li>
<li>dotted line:     &#8216;:&#8217;</li>
<li>dash-dot line:   &#8216;-.&#8217;</li>
</ul>
</dd>
</dl>
<p>During programming, you can find all these details in the
documentation of the plot function. Just type help(plot)
in an interactive Python shell or invoke pydoc with
scitools.easyviz.plot. This tutorial is available
through pydoc scitools.easyviz.</p>
<p>We remark that in the Gnuplot program all the different line types are
drawn as solid lines on the screen. The hardcopy chooses automatically
different line types (solid, dashed, etc.) and not in accordance with
the line type specification.</p>
<dl class="docutils">
<dt>Lots of markers at data points are available:</dt>
<dd><ul class="first last simple">
<li>plus sign:                     &#8216;+&#8217;</li>
<li>circle:                        &#8216;o&#8217;</li>
<li>asterisk:                      &#8216;*&#8217;</li>
<li>point:                         &#8216;.&#8217;</li>
<li>cross:                         &#8216;x&#8217;</li>
<li>square:                        &#8216;s&#8217;</li>
<li>diamond:                       &#8216;d&#8217;</li>
<li>upward-pointing triangle:      &#8216;^&#8217;</li>
<li>downward-pointing triangle:    &#8216;v&#8217;</li>
<li>right-pointing triangle:       &#8216;&gt;&#8217;</li>
<li>left-pointing triangle:        &#8216;&lt;&#8217;</li>
<li>five-point star (pentagram):   &#8216;p&#8217;</li>
<li>six-point star (hexagram):     &#8216;h&#8217;</li>
<li>no marker (default): None</li>
</ul>
</dd>
</dl>
<p>Symbols and line styles may be combined, for instance as in &#8216;kx-&#8216;,
which means a black solid line with black crosses at the data points.</p>
<p><em>Another Example.</em> Let us extend the previous example with a third
curve where the data points are slightly randomly distributed around
the f_2(t) curve:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>

<span class="k">def</span> <span class="nf">f1</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">f2</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">f1</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>

<span class="n">t</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">51</span><span class="p">)</span>
<span class="n">y1</span> <span class="o">=</span> <span class="n">f1</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="n">y2</span> <span class="o">=</span> <span class="n">f2</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>

<span class="c"># Pick out each 4 points and add random noise</span>
<span class="n">t3</span> <span class="o">=</span> <span class="n">t</span><span class="p">[::</span><span class="mi">4</span><span class="p">]</span>      <span class="c"># slice, stride 4</span>
<span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">11</span><span class="p">)</span>  <span class="c"># fix random sequence</span>
<span class="n">noise</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">0.02</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">t3</span><span class="p">))</span>
<span class="n">y3</span> <span class="o">=</span> <span class="n">y2</span><span class="p">[::</span><span class="mi">4</span><span class="p">]</span> <span class="o">+</span> <span class="n">noise</span>

<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="s">&#39;r-&#39;</span><span class="p">)</span>
<span class="n">hold</span><span class="p">(</span><span class="s">&#39;on&#39;</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y2</span><span class="p">,</span> <span class="s">&#39;ks-&#39;</span><span class="p">)</span>   <span class="c"># black solid line with squares at data points</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t3</span><span class="p">,</span> <span class="n">y3</span><span class="p">,</span> <span class="s">&#39;bo&#39;</span><span class="p">)</span>

<span class="n">legend</span><span class="p">(</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;t^4*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;data&#39;</span><span class="p">)</span>
<span class="n">title</span><span class="p">(</span><span class="s">&#39;Simple Plot Demo&#39;</span><span class="p">)</span>
<span class="n">axis</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.05</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">])</span>
<span class="n">xlabel</span><span class="p">(</span><span class="s">&#39;t&#39;</span><span class="p">)</span>
<span class="n">ylabel</span><span class="p">(</span><span class="s">&#39;y&#39;</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
<span class="n">savefig</span><span class="p">(</span><span class="s">&#39;tmp3.eps&#39;</span><span class="p">)</span>   <span class="c"># or hardcopy</span>
<span class="n">savefig</span><span class="p">(</span><span class="s">&#39;tmp3.png&#39;</span><span class="p">)</span>   <span class="c"># or hardcopy</span>
</pre></div>
</div>
<p>The plot is shown in Figure ref{fig:plot3}.</p>
<p>FIGURE:[figs/plot3.eps] A plot with three curves.  {fig:plot3}</p>
<p><em>Minimalistic Typing.</em> When exploring mathematics in the interactive Python shell, most of us
are interested in the quickest possible commands.
Here is an example of minimalistic syntax for
comparing the two sample functions we have used in the previous examples:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">t</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">51</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">),</span> <span class="n">t</span><span class="p">,</span> <span class="n">t</span><span class="o">**</span><span class="mi">4</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
</pre></div>
</div>
<p><em>Text.</em> A text can be placed at a point (x,y) using the call:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">text</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s">&#39;Some text&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p><em>More Examples.</em> The examples in this tutorial, as well as
additional examples, can be found in the examples directory in the
root directory of the SciTools source code tree.</p>
</div>
<div class="section" id="interactive-plotting-sessions">
<h3>Interactive Plotting Sessions<a class="headerlink" href="#interactive-plotting-sessions" title="Permalink to this headline">¶</a></h3>
<p>All the Easyviz commands can of course be issued in an interactive
Python session. The only thing to comment is that the plot command
returns a result:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">51</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="go">[&lt;scitools.easyviz.common.Line object at 0xb5727f6c&gt;]</span>
</pre></div>
</div>
<p>Most users will just ignore this output line.</p>
<p>All Easyviz commands that produce a plot return an object reflecting the
particular type of plot. The plot command returns a list of
Line objects, one for each curve in the plot. These Line
objects can be invoked to see, for instance, the value of different
parameters in the plot:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">line</span><span class="p">,</span> <span class="o">=</span> <span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getp</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="go">{&#39;description&#39;: &#39;&#39;,</span>
<span class="go"> &#39;dims&#39;: (4, 1, 1),</span>
<span class="go"> &#39;legend&#39;: &#39;&#39;,</span>
<span class="go"> &#39;linecolor&#39;: &#39;b&#39;,</span>
<span class="go"> &#39;pointsize&#39;: 1.0,</span>
<span class="go"> ...</span>
</pre></div>
</div>
<p>Such output is mostly of interest to advanced users.</p>
</div>
<div class="section" id="making-animations">
<h3>Making Animations<a class="headerlink" href="#making-animations" title="Permalink to this headline">¶</a></h3>
<p>A sequence of plots can be combined into an animation and stored in a
movie file. First we need to generate a series of hardcopies, i.e.,
plots stored in files.  Thereafter we must use a tool to combine the
individual plot files into a movie file.</p>
<p><em>Example.</em> The function
f(x; m,s) = 1/(sqrt(2*pi)*s)*exp(-0.5*((x-m)/s)**2) 
is known as the Gaussian function or the probability density function
of the normal (or Gaussian) distribution.  This bell-shaped function is
&#8220;wide&#8221; for large s and &#8220;peak-formed&#8221; for small s, see Figure
ref{fig:plot4}. The function is symmetric around x=m (m=0 in the
figure).  Our goal is to make an animation where we see how this
function evolves as s is decreased. In Python we implement the
formula above as a function f(x, m, s).</p>
<p>FIGURE:[figs/plot4.eps] Different shapes of a Gaussian function. {fig:plot4}</p>
<p>The animation is created by varying s in a loop and for each s
issue a plot command. A moving curve is then visible on the screen.
One can also make a movie file that can be played as any other
computer movie using a standard movie player. To this end, each plot
is saved to a file, and all the files are combined together using some
suitable tool, which is reached through the movie function in
Easyviz. All necessary steps will be apparent in the complete program
below, but before diving into the code we need to comment upon a
couple of issues with setting up the plot command for animations.</p>
<p>The underlying plotting program will normally adjust the y axis to the
maximum and minimum values of the curve if we do not specify the axis
ranges explicitly. For an animation such automatic axis adjustment is
misleading - the axis ranges must be fixed to avoid a jumping
axis. The relevant values for the axis range is the minimum and
maximum value of f. The minimum value is zero, while the maximum
value appears for x=m and increases with decreasing s. The range
of the y axis must therefore be [0,f(m; m, min s)].</p>
<p>The function f is defined for all -infty &lt; x &lt; infty, but the
function value is very small already 3s away from x=m. We may therefore
limit the x coordinates to [m-3s,m+3s].</p>
<p>Now we are ready to take a look at the complete code
for animating how the Gaussian function evolves as the s parameter
is decreased from 2 to 0.2:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">s</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">pi</span><span class="p">)</span><span class="o">*</span><span class="n">s</span><span class="p">))</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span><span class="o">*</span><span class="p">((</span><span class="n">x</span><span class="o">-</span><span class="n">m</span><span class="p">)</span><span class="o">/</span><span class="n">s</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>

<span class="n">m</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">s_start</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">s_stop</span> <span class="o">=</span> <span class="mf">0.2</span>
<span class="n">s_values</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="n">s_start</span><span class="p">,</span> <span class="n">s_stop</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="n">m</span> <span class="o">-</span><span class="mi">3</span><span class="o">*</span><span class="n">s_start</span><span class="p">,</span> <span class="n">m</span> <span class="o">+</span> <span class="mi">3</span><span class="o">*</span><span class="n">s_start</span><span class="p">,</span> <span class="mi">1000</span><span class="p">)</span>
<span class="c"># f is max for x=m; smaller s gives larger max value</span>
<span class="n">max_f</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">s_stop</span><span class="p">)</span>

<span class="c"># Show the movie on the screen</span>
<span class="c"># and make hardcopies of frames simultaneously</span>
<span class="n">counter</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">s_values</span><span class="p">:</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
    <span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">x</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="o">-</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">max_f</span><span class="p">],</span>
         <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;x&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;f&#39;</span><span class="p">,</span> <span class="n">legend</span><span class="o">=</span><span class="s">&#39;s=</span><span class="si">%4.2f</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">s</span><span class="p">,</span>
         <span class="n">hardcopy</span><span class="o">=</span><span class="s">&#39;tmp</span><span class="si">%04d</span><span class="s">.png&#39;</span> <span class="o">%</span> <span class="n">counter</span><span class="p">)</span>
    <span class="n">counter</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="c">#time.sleep(0.2)  # can insert a pause to control movie speed</span>

<span class="c"># Make movie file the simplest possible way</span>
<span class="n">movie</span><span class="p">(</span><span class="s">&#39;tmp*.png&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that the s values are decreasing (linspace handles this
automatically if the start value is greater than the stop value).
Also note that we, simply because we think it is visually more
attractive, let the y axis go from -0.1 although the f function is
always greater than zero.</p>
<p><em>Remarks on Filenames.</em> For each frame (plot) in the movie we store the plot in a file.  The
different files need different names and an easy way of referring to
the set of files in right order. We therefore suggest to use filenames
of the form tmp0001.png, tmp0002.png, tmp0003.png, etc.  The
printf format 04d pads the integers with zeros such that 1 becomes
0001, 13 becomes 0013 and so on.  The expression tmp*.png will
now expand (by an alphabetic sort) to a list of all files in proper
order. Without the padding with zeros, i.e., names of the form
tmp1.png, tmp2.png, ..., tmp12.png, etc., the alphabetic order
will give a wrong sequence of frames in the movie. For instance, 
tmp12.png will appear before tmp2.png.</p>
<p>Note that the names of plot files specified when making hardopies must
be consistent with the specification of names in the call to movie.
Typically, one applies a Unix wildcard notation in the call to
movie, say plotfile*.eps, where the asterisk will match any set of
characters. When specifying hardcopies, we must then use a filename
that is consistent with plotfile*.eps, that is, the filename must
start with plotfile and end with .eps, but in between
these two parts we are free to construct (e.g.) a frame number padded
with zeros.</p>
<p>We recommend to always remove previously generated plot files before
a new set of files is made. Otherwise, the movie may get old and new
files mixed up. The following Python code removes all files
of the form tmp*.png:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">glob</span><span class="o">,</span> <span class="nn">os</span>
<span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s">&#39;tmp*.png&#39;</span><span class="p">):</span>
    <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
</pre></div>
</div>
<p>These code lines should be inserted at the beginning of the code example
above. Alternatively, one may store all plotfiles in a subfolder
and later delete the subfolder. Here is a suitable code segment:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">shutil</span><span class="o">,</span> <span class="nn">os</span>
<span class="n">subdir</span> <span class="o">=</span> <span class="s">&#39;temp&#39;</span>            <span class="c"># name of subfolder for plot files</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">subdir</span><span class="p">):</span>  <span class="c"># does the subfolder already exist?</span>
    <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="n">subdir</span><span class="p">)</span>  <span class="c"># delete the whole folder</span>
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">subdir</span><span class="p">)</span>           <span class="c"># make new subfolder</span>
<span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">subdir</span><span class="p">)</span>           <span class="c"># move to subfolder</span>
<span class="c"># ...perform all the plotting...</span>
<span class="c"># ...make movie...</span>
<span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">pardir</span><span class="p">)</span>        <span class="c"># optional: move up to parent folder</span>
</pre></div>
</div>
<p><em>Movie Formats.</em> Having a set of (e.g.) tmp*.png files, one can simply generate a movie by
a movie(&#8216;tmp*.png&#8217;) call. The movie function generates a movie
file called movie.avi (AVI format), movie.mpeg (MPEG format), or
movie.gif (animated GIF format) in the current working
directory. The movie format depends on the encoders found on your
machine.</p>
<p>You can get complete control of the movie format and the
name of the movie file by supplying more arguments to the
movie function. First, let us generate an animated GIF
file called tmpmovie.gif:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">movie</span><span class="p">(</span><span class="s">&#39;tmp_*.eps&#39;</span><span class="p">,</span> <span class="n">encoder</span><span class="o">=</span><span class="s">&#39;convert&#39;</span><span class="p">,</span> <span class="n">fps</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
      <span class="n">output_file</span><span class="o">=</span><span class="s">&#39;tmpmovie.gif&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The generation of animated GIF images applies the convert program
from the ImageMagick suite. This program must of course be installed
on the machine. The argument fps stands for frames per second so
here the speed of the movie is slow in that there is a delay of half
a second between each frame (image file).
To view the animated GIF file, one can use the animate
program (also from ImageMagick) and give the movie file as command-line
argument. One can alternatively put the GIF file in a web page 
in an IMG tag such that a browser automatically displays the movie.</p>
<p>An AVI movie can be generated by the call:</p>
<div class="highlight-python"><pre>movie('tmp_*.eps', encoder='ffmpeg', fps=4,
      output_file='tmpmovie1.avi',</pre>
</div>
<p>Alternatively, we may generate an MPEG movie using 
the ppmtompeg encoder from the Netpbm suite of
image manipulation tools:</p>
<div class="highlight-python"><pre>movie('tmp_*.eps', encoder='ppmtompeg', fps=24,
      output_file='tmpmovie2.mpeg',</pre>
</div>
<p>The ppmtompeg supports only a few (high) frame rates.</p>
<p>The next sample call to movie uses the Mencoder tool and specifies 
some additional arguments (video codec, video bitrate, and the 
quantization scale):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">movie</span><span class="p">(</span><span class="s">&#39;tmp_*.eps&#39;</span><span class="p">,</span> <span class="n">encoder</span><span class="o">=</span><span class="s">&#39;mencoder&#39;</span><span class="p">,</span> <span class="n">fps</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span>
      <span class="n">output_file</span><span class="o">=</span><span class="s">&#39;tmpmovie.mpeg&#39;</span><span class="p">,</span>
      <span class="n">vcodec</span><span class="o">=</span><span class="s">&#39;mpeg2video&#39;</span><span class="p">,</span> <span class="n">vbitrate</span><span class="o">=</span><span class="mi">2400</span><span class="p">,</span> <span class="n">qscale</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
</pre></div>
</div>
<p>Playing movie files can be done by a lot of programs. Windows Media
Player is a default choice on Windows machines. On Unix, a variety
of tools can be used. For animated GIF files the animate program
from the ImageMagick suite is suitable, or one can simply
show the file in a web page with the HTML command
&lt;img src=&#8221;tmpmovie.gif&#8221;&gt;. AVI and MPEG files can be played by,
for example, the
myplayer, vlc, or totem programs.</p>
</div>
<div class="section" id="moving-plot-window">
<h3>Moving Plot Window<a class="headerlink" href="#moving-plot-window" title="Permalink to this headline">¶</a></h3>
<p>When calculating long time series, it may be desirable to have a
moving plot window that follows the time series. The module
MovingPlotWindow was made for this purpose. There are three
different modes of this tool, where each mode moves the window
in a certain way. With mode set as continuous movement,
the plot window moves with the curves continuously.
With mode set as continuous drawing, the curves are drawn
from left to right in the plot window, as an animation (one step
at a time). When the curves reach the right border of the plot window,
the window (or more correctly, the x-axis) is moved in a jump
to the right so that the curves are coming in from the left border
again. With mode set as jumps the curves are plotted directly
in the window and shown for a specified period of time (the pause
parameter), then the axis jump one window to the right, and the
curves are displayed in this (time) window. The jumps mode is
well suited for quickly browsing a time series. The continuous
drawing mode is aimed at studing the &#8220;tip&#8221; of the time series
as they are computed, and continuous movement is a kind of
default choice for most purposes. Running the module file gives
a demo of the three modes.</p>
<p>Below is an example of how to compute a time series by finite
differences and comparing this series with the exact solutions.
For large times, there is a fequency discrepancy that one wants
to investigate:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">_demo</span><span class="p">(</span><span class="n">I</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">dt</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s">&#39;continuous movement&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Solve u&#39; = -k**2*u, u(0)=I, u&#39;(0)=0 by a finite difference</span>
<span class="sd">    method with time steps dt, from t=0 to t=T.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">dt</span> <span class="o">&gt;</span> <span class="mf">2.</span><span class="o">/</span><span class="n">k</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&#39;Unstable scheme&#39;</span>
    <span class="n">N</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">T</span><span class="o">/</span><span class="nb">float</span><span class="p">(</span><span class="n">dt</span><span class="p">)))</span>
    <span class="n">u</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">(</span><span class="n">N</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">t</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">N</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>

    <span class="n">umin</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1.2</span><span class="o">*</span><span class="n">I</span>
    <span class="n">umax</span> <span class="o">=</span> <span class="o">-</span><span class="n">umin</span>
    <span class="n">period</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">pi</span><span class="o">/</span><span class="n">k</span>  <span class="c"># period of the oscillations</span>
    <span class="n">plot_manager</span> <span class="o">=</span> <span class="n">MovingPlotWindow</span><span class="p">(</span><span class="mi">8</span><span class="o">*</span><span class="n">period</span><span class="p">,</span> <span class="n">dt</span><span class="p">,</span> <span class="n">yaxis</span><span class="o">=</span><span class="p">[</span><span class="n">umin</span><span class="p">,</span> <span class="n">umax</span><span class="p">],</span>
                                    <span class="n">mode</span><span class="o">=</span><span class="n">mode</span><span class="p">)</span>
    <span class="n">u</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">I</span>
    <span class="n">u</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">u</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mf">0.5</span><span class="o">*</span><span class="n">dt</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">k</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">u</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">N</span><span class="p">):</span>
        <span class="n">u</span><span class="p">[</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">u</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">-</span> <span class="n">u</span><span class="p">[</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">dt</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">k</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">u</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">plot_manager</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">s</span> <span class="o">=</span> <span class="n">plot_manager</span><span class="o">.</span><span class="n">first_index_in_plot</span>
            <span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">s</span><span class="p">:</span><span class="n">n</span><span class="o">+</span><span class="mi">2</span><span class="p">],</span> <span class="n">u</span><span class="p">[</span><span class="n">s</span><span class="p">:</span><span class="n">n</span><span class="o">+</span><span class="mi">2</span><span class="p">],</span> <span class="s">&#39;r-&#39;</span><span class="p">,</span>
                 <span class="n">t</span><span class="p">[</span><span class="n">s</span><span class="p">:</span><span class="n">n</span><span class="o">+</span><span class="mi">2</span><span class="p">],</span> <span class="n">I</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">k</span><span class="o">*</span><span class="n">t</span><span class="p">)[</span><span class="n">s</span><span class="p">:</span><span class="n">n</span><span class="o">+</span><span class="mi">2</span><span class="p">],</span> <span class="s">&#39;b-&#39;</span><span class="p">,</span>
                 <span class="n">axis</span><span class="o">=</span><span class="n">plot_manager</span><span class="o">.</span><span class="n">axis</span><span class="p">(),</span>
                 <span class="n">title</span><span class="o">=</span><span class="s">&quot;Solution of u&#39;&#39; + k^2 u = 0 for t=</span><span class="si">%6.3f</span><span class="s"> (mode: </span><span class="si">%s</span><span class="s">)&quot;</span>                          <span class="o">%</span> <span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="n">mode</span><span class="p">))</span>
        <span class="n">plot_manager</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</pre></div>
</div>
<p>An appropriate import statement is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.MovingPlotWindow</span> <span class="kn">import</span> <span class="n">MovingPlotWindow</span>
</pre></div>
</div>
</div>
<div class="section" id="advanced-easyviz-topics">
<h3>Advanced Easyviz Topics<a class="headerlink" href="#advanced-easyviz-topics" title="Permalink to this headline">¶</a></h3>
<p>The information in the previous sections aims at being sufficient for
the daily work with plotting curves. Sometimes, however, one wants to
fine-control the plot or how Easyviz behaves. First, we explain how to
set the backend. Second, we tell how to speed up the 
from scitools.std import * statement.  Third, we show how to operate with
the plotting program directly and using plotting program-specific
advanced features. Fourth, we explain how the user can grab Figure
and Axis objects that Easyviz produces &#8220;behind the curtain&#8221;.</p>
<div class="section" id="controlling-the-backend">
<h4>Controlling the Backend<a class="headerlink" href="#controlling-the-backend" title="Permalink to this headline">¶</a></h4>
<p>The Easyviz backend can either be set in a configuration file (see 
&#8220;Setting Parameters in the Configuration File&#8221; below), by
importing a special backend in the program, or by adding a
command-line option:</p>
<div class="highlight-python"><pre>--SCITOOLS_easyviz_backend name</pre>
</div>
<p>where name is the name of the backend: gnuplot, vtk,
matplotlib, etc. Which backend you choose depends on what you have
available on your computer system and what kind of plotting
functionality you want.</p>
<p>An alternative method is to import a specific backend in a program. Instead
of the from scitools.std import * statement one writes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">scitools.easyviz.gnuplot_</span> <span class="kn">import</span> <span class="o">*</span>  <span class="c"># work with Gnuplot</span>
<span class="c"># or</span>
<span class="kn">from</span> <span class="nn">scitools.easyviz.vtk_</span> <span class="kn">import</span> <span class="o">*</span>      <span class="c"># work with VTK</span>
</pre></div>
</div>
<p>Note the trailing underscore in the module names for the various backends.</p>
<p>The following program prints a list of the names of the
available backends on your computer system:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>
<span class="n">backends</span> <span class="o">=</span> <span class="n">available_backends</span><span class="p">()</span>
<span class="k">print</span> <span class="s">&#39;Available backends:&#39;</span><span class="p">,</span> <span class="n">backends</span>
</pre></div>
</div>
<p>There will be quite some output explaining the missing backends and
what must be installed to use these backends. Be prepared for exceptions
and error messages too.</p>
</div>
<div class="section" id="importing-just-easyviz">
<h4>Importing Just Easyviz<a class="headerlink" href="#importing-just-easyviz" title="Permalink to this headline">¶</a></h4>
<p>The from scitools.std import * statement imports many modules and packages:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="o">*</span>    
<span class="kn">from</span> <span class="nn">scitools.numpyutils</span> <span class="kn">import</span> <span class="o">*</span>  <span class="c"># some convenience functions</span>
<span class="kn">from</span> <span class="nn">numpy.lib.scimath</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">scipy</span> <span class="kn">import</span> <span class="o">*</span>                <span class="c"># if scipy is installed</span>
<span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">operator</span><span class="o">,</span> <span class="nn">math</span>
<span class="kn">from</span> <span class="nn">scitools.StringFunction</span> <span class="kn">import</span> <span class="n">StringFunction</span>
<span class="kn">from</span> <span class="nn">glob</span> <span class="kn">import</span> <span class="n">glob</span>
</pre></div>
</div>
<p>The scipy import can take some time and lead to slow start-up of plot 
scripts. A more minimalistic import for curve plotting is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.easyviz</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>Alternatively, one can edit the SciTools configuration file as
explained below in the section &#8220;Setting Parameters in the
Configuration File&#8221;.</p>
<p>Many discourage the use of &#8220;star import&#8221; as shown above. For example,
the standard import of Numerical Python in all of its documentation is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
</pre></div>
</div>
<p>A similar import for SciTools and Easyviz is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">scitools.std</span> <span class="kn">as</span> <span class="nn">st</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
</pre></div>
</div>
<p>Although np functions are important into the namespace of st in
this case, we recommend to distinguish the packages when using a prefix.
A typical plotting example will then read:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">51</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">)</span>
<span class="n">st</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s">&#39;r-&#39;</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s">&quot;Plot&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>The corresponding syntax for the
minimalistic import of scitools.easyviz and numpy reads:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">scitools.easyviz</span> <span class="kn">as</span> <span class="nn">ev</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
</pre></div>
</div>
</div>
<div class="section" id="setting-parameters-in-the-configuration-file">
<h4>Setting Parameters in the Configuration File<a class="headerlink" href="#setting-parameters-in-the-configuration-file" title="Permalink to this headline">¶</a></h4>
<p>Easyviz is a subpackage of SciTools, and the the SciTools
configuration file, called scitools.cfg has several sections
([easyviz], [gnuplot], and [matplotlib]) where parameters
controlling the behavior of plotting can be set. For example, the
backend for Easyviz can be controlled with the backend parameter:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="n">easyviz</span><span class="p">]</span>
<span class="n">backend</span> <span class="o">=</span> <span class="n">vtk</span>
</pre></div>
</div>
<p>Similarly, Matplotlib&#8217;s use of LaTeX can be controlled by a boolean
parameter:</p>
<div class="highlight-python"><pre>[matplotlib]
text.usetex = &lt;bool&gt; false</pre>
</div>
<p>The text &lt;bool&gt; indicates that this is a parameter with a boolean</p>
<p>A configuration file with name .scitools.cfg file can be placed in
the current working folder, thereby affecting plots made in this
folder, or it can be located in the user&#8217;s home folder, which will
affect all plotting sessions for the user in question. There is also a
common SciTools config file scitools.cfg for the whole site, located
in the directory where the scitools package is installed. It is 
recommended to copy the scitools.cfg, either from installation or
the SciTools source folder lib/scitools, to .scitools.cfg
in your home folder. Then you can easily control the Easyviz backend
and other paramteres by editing your local .scitools.cfg file.</p>
<p>Parameters set in the configuration file can also be set directly
on the command line when running a program. The name of the
command-line option is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="o">--</span><span class="n">SCITOOLS_sectionname_parametername</span>
</pre></div>
</div>
<p>where sectionname is the name of the section in the file
and parametername is the name of the
parameter. For example, setting the backend parameter in the
[easyviz] section by:</p>
<div class="highlight-python"><pre>--SCITOOLS_easyviz_backend gnuplot</pre>
</div>
<p>Here is an example where we use Matplotlib as backend, turn on
the use of LaTeX in Matplotlib, and avoid the potentially slow import
of SciPy:</p>
<div class="highlight-python"><pre>python myprogram.py --SCITOOLS_easyviz_backend matplotlib \ 
    --SCITOOLS_matplotlib_text.usetex true --SCITOOLS_scipy_load no</pre>
</div>
</div>
<div class="section" id="working-with-the-plotting-program-directly">
<h4>Working with the Plotting Program Directly<a class="headerlink" href="#working-with-the-plotting-program-directly" title="Permalink to this headline">¶</a></h4>
<p>Easyviz supports just the most common plotting commands, typically the
commands you use &#8220;95 percent&#8221; of the time when exploring curves.
Various plotting packages have lots of additional commands for
different advanced features.  When Easyviz does not have a command
that supports a particular feature, one can grab the Python object
that communicates with the underlying plotting program (known as
&#8220;backend&#8221;) and work with this object directly, using plotting
program-specific command syntax.  Let us illustrate this principle
with an example where we add a text and an arrow in the plot, see
Figure ref{fig:plot2i}.</p>
<p>FIGURE:[figs/plot2i.eps] Illustration of a text and an arrow using Gnuplot-specific commands. {fig:plot2i}</p>
<p>Easyviz does not support arrows at arbitrary places inside the plot,
but Gnuplot does. If we use Gnuplot as backend, we may grab the
Gnuplot object and issue Gnuplot commands to this object
directly. Here is an example of the typical recipe, written after the
core of the plot is made in the ordinary (plotting
program-independent) way:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="n">backend</span> <span class="o">==</span> <span class="s">&#39;gnuplot&#39;</span><span class="p">:</span>
    <span class="n">g</span> <span class="o">=</span> <span class="n">get_backend</span><span class="p">()</span>
    <span class="c"># g is a Gnuplot object, work with Gnuplot commands directly:</span>
    <span class="n">g</span><span class="p">(</span><span class="s">&#39;set label &quot;global maximum&quot; at 0.1,0.5 font &quot;Times,18&quot;&#39;</span><span class="p">)</span>
    <span class="n">g</span><span class="p">(</span><span class="s">&#39;set arrow from 0.5,0.48 to 0.98,0.37 linewidth 2&#39;</span><span class="p">)</span>
    <span class="n">g</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
    <span class="n">g</span><span class="o">.</span><span class="n">hardcopy</span><span class="p">(</span><span class="s">&#39;tmp2.eps&#39;</span><span class="p">)</span>  <span class="c"># make new hardcopy</span>

    <span class="n">g</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>               <span class="c"># new plot</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">Gnuplot</span><span class="o">.</span><span class="n">Data</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">t</span><span class="o">**</span><span class="mi">3</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="p">),</span> <span class="n">with_</span><span class="o">=</span><span class="s">&#39;points 3 3&#39;</span><span class="p">,</span>
                        <span class="n">title</span><span class="o">=</span><span class="s">&#39;t**3*exp(-t)&#39;</span><span class="p">)</span>
    <span class="n">func</span> <span class="o">=</span> <span class="n">Gnuplot</span><span class="o">.</span><span class="n">Func</span><span class="p">(</span><span class="s">&#39;t**4*exp(-t)&#39;</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s">&#39;t**4*exp(-t)&#39;</span><span class="p">)</span>
    <span class="n">g</span><span class="p">(</span><span class="s">&#39;set tics border font &quot;Courier,14&quot;&#39;</span><span class="p">)</span>
    <span class="n">g</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
</pre></div>
</div>
<p>For the available features and the syntax of commands, we refer to
the Gnuplot manual and the demo.py program in Python interface to
Gnuplot.</p>
<p>Here is an example with Matplotlib:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="n">backend</span> <span class="o">==</span> <span class="s">&#39;matplotlib&#39;</span><span class="p">:</span>
    <span class="n">pyplot</span> <span class="o">=</span> <span class="n">get_backend</span><span class="p">()</span>
    <span class="c"># Work with standard matplotlib.pyplot functions</span>
</pre></div>
</div>
<p>The files grab_backend*.py in the examples folder of the SciTools
source code contain many examples on how to do backend-specific operations,
especially with Matplotlib.</p>
<p>The idea advocated here is that you can quickly generate
plots with Easyviz using standard commands that are independent of
the underlying plotting package. However, when you need advanced
features, you must add plotting package-specific code as shown
above. This principle makes Easyviz a light-weight interface, but
without limiting the available functionality of various plotting programs.</p>
</div>
<div class="section" id="working-with-axis-and-figure-objects">
<h4>Working with Axis and Figure Objects<a class="headerlink" href="#working-with-axis-and-figure-objects" title="Permalink to this headline">¶</a></h4>
<p>Easyviz supports the concept of Axis objects, as in Matlab.
The Axis object represents a set of axes, with curves drawn in the
associated coordinate system. A figure is the complete physical plot.
One may have several axes in one figure, each axis representing a subplot.
One may also have several figures, represented by different
windows on the screen or separate hardcopies.</p>
<p>Users with Matlab experience may prefer to set axis
labels, ranges, and the title using an Axis object instead of
providing the information in separate commands or as part of a plot
command. The gca (get current axis) command returns an Axis
object, whose set method can be used to set axis properties:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="s">&#39;r-&#39;</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">y2</span><span class="p">,</span> <span class="s">&#39;bo&#39;</span><span class="p">,</span>
     <span class="n">legend</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">,</span> <span class="s">&#39;t^4*exp(-t^2)&#39;</span><span class="p">),</span>
     <span class="n">savefig</span><span class="o">=</span><span class="s">&#39;tmp2.eps&#39;</span><span class="p">)</span>

<span class="n">ax</span> <span class="o">=</span> <span class="n">gca</span><span class="p">()</span>   <span class="c"># get current Axis object</span>
<span class="n">ax</span><span class="o">.</span><span class="n">setp</span><span class="p">(</span><span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;y&#39;</span><span class="p">,</span>
        <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">],</span>
        <span class="n">title</span><span class="o">=</span><span class="s">&#39;Plotting two curves in the same plot&#39;</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>  <span class="c"># show the plot again after ax.setp actions</span>
</pre></div>
</div>
<p>The figure() call makes a new figure, i.e., a
new window with curve plots. Figures are numbered as 1, 2, and so on.
The command figure(3) sets the current figure object to figure number
3.</p>
<p>Suppose we want to plot our y1 and y2 data in two separate windows.
We need in this case to work with two Figure objects:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="s">&#39;r-&#39;</span><span class="p">,</span> <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;y&#39;</span><span class="p">,</span>
     <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">])</span>

<span class="n">figure</span><span class="p">()</span>  <span class="c"># new figure</span>

<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y2</span><span class="p">,</span> <span class="s">&#39;bo&#39;</span><span class="p">,</span> <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;y&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>We may now go back to the first figure (with the y1 data) and
set a title and legends in this plot, show the plot, and make a PostScript
version of the plot:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">figure</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>  <span class="c"># go back to first figure</span>
<span class="n">title</span><span class="p">(</span><span class="s">&#39;One curve&#39;</span><span class="p">)</span>
<span class="n">legend</span><span class="p">(</span><span class="s">&#39;t^2*exp(-t^2)&#39;</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
<span class="n">savefig</span><span class="p">(</span><span class="s">&#39;tmp2_1.eps&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>We can also adjust figure 2:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">figure</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>  <span class="c"># go to second figure</span>
<span class="n">title</span><span class="p">(</span><span class="s">&#39;Another curve&#39;</span><span class="p">)</span>
<span class="n">savefig</span><span class="p">(</span><span class="s">&#39;tmp2_2.eps&#39;</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<p>The current Figure object is reached by gcf (get current figure),
and the dump method dumps the internal parameters in the Figure
object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">fig</span> <span class="o">=</span> <span class="n">gcf</span><span class="p">();</span> <span class="k">print</span> <span class="n">fig</span><span class="o">.</span><span class="n">dump</span><span class="p">()</span>
</pre></div>
</div>
<p>These parameters may be of interest for troubleshooting when Easyviz
does not produce what you expect.</p>
<p>Let us then make a third figure with two plots, or more precisely, two
axes: one with y1 data and one with y2 data.
Easyviz has a command subplot(r,c,a) for creating r
rows and c columns and set the current axis to axis number a.
In the present case subplot(2,1,1) sets the current axis to
the first set of axis in a &#8220;table&#8221; with two rows and one column.
Here is the code for this third figure:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">figure</span><span class="p">()</span>  <span class="c"># new, third figure</span>
<span class="c"># Plot y1 and y2 as two axis in the same figure</span>
<span class="n">subplot</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;y&#39;</span><span class="p">)</span>
<span class="n">subplot</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">y2</span><span class="p">,</span> <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;y&#39;</span><span class="p">)</span>
<span class="n">title</span><span class="p">(</span><span class="s">&#39;A figure with two plots&#39;</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
<span class="n">savefig</span><span class="p">(</span><span class="s">&#39;tmp2_3.eps&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>If we need to place an axis at an arbitrary position in the figure, we
must use the command:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ax</span> <span class="o">=</span> <span class="n">axes</span><span class="p">(</span><span class="n">viewport</span><span class="o">=</span><span class="p">[</span><span class="n">left</span><span class="p">,</span> <span class="n">bottom</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">height</span><span class="p">])</span>
</pre></div>
</div>
<p>The four parameteres left, bottom, width, height
are location values between 0 and 1 ((0,0) is the lower-left corner 
and (1,1) is the upper-right corner). However, this might be a bit
different in the different backends (see the documentation for the
backend in question).</p>
</div>
<div class="section" id="turning-off-all-plotting">
<h4>Turning Off All Plotting<a class="headerlink" href="#turning-off-all-plotting" title="Permalink to this headline">¶</a></h4>
<p>Sometimes, especially during debugging or when trying out a large-scale
experiment, it is nice to turn off all plotting on the screen and
all making of hardcopies. This is easily done by:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">turn_off_plotting</span><span class="p">(</span><span class="nb">globals</span><span class="p">())</span>
</pre></div>
</div>
<p>All the plot functions now &#8220;do nothing&#8221; (actually they are DoNothing
objects from scitools.misc).</p>
</div>
</div>
</div>
<div class="section" id="visualization-of-scalar-fields">
<h2>Visualization of Scalar Fields<a class="headerlink" href="#visualization-of-scalar-fields" title="Permalink to this headline">¶</a></h2>
<p>A scalar field is a function from space or space-time to a real value.
This real value typically reflects a scalar physical parameter at every
point in space (or in space and time). One example is temperature,
which is a scalar quantity defined everywhere in space and time.  In a
visualization context, we work with discrete scalar fields that are
defined on a grid. Each point in the grid is then associated with a
scalar value.</p>
<p>There are several ways to visualize a scalar field in Easyviz. Both
two- and three-dimensional scalar fields are supported. In two
dimensions (2D) we can create elevated surface plots, contour plots,
and pseudocolor plots, while in three dimensions (3D) we can create
isosurface plots, volumetric slice plots, and contour slice plots.</p>
<div class="section" id="elevated-surface-plots">
<h3>Elevated Surface Plots<a class="headerlink" href="#elevated-surface-plots" title="Permalink to this headline">¶</a></h3>
<p>To create elevated surface plots we can use either the surf or the
mesh command. Both commands have the same syntax, but the mesh
command creates a wireframe mesh while the surf command creates a
solid colored surface.</p>
<p>Our examples will make use of the scalar field
f(x,y)=sin(r), 
where r is the distance in the plane from the origin, i.e.,
r=sqrt(x**2+y**2).
The x and y values in our 2D domain lie between -5 and 5.</p>
<p>The example first creates the necessary data arrays for 2D scalar
field plotting: the coordinates in each direction, extensions of these
arrays to form a <em>ndgrid</em>, and the function values. The latter array
is computed in a vectorized operation which requires the extended
coordinate arrays from the ndgrid function.  The mesh command
can then produce the plot with a syntax that mirrors the simplicity of
the plot command for curves:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">21</span><span class="p">)</span>
<span class="n">xv</span><span class="p">,</span> <span class="n">yv</span> <span class="o">=</span> <span class="n">ndgrid</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">values</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">xv</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">yv</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="n">h</span> <span class="o">=</span> <span class="n">mesh</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span>
</pre></div>
</div>
<p>The mesh command returns a reference to a new Surface object, here
stored in a variable h. This reference can be used to set or get
properties in the object at a later stage if needed.  The resulting
plot can be seen in Figure ref{fig:mesh_ex1}.</p>
<p>We remark that the computations in the previous example are vectorized.
The corresponding scalar computations using a double loop read:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">values</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">size</span><span class="p">,</span> <span class="n">y</span><span class="o">.</span><span class="n">size</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">size</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">size</span><span class="p">):</span>
        <span class="n">values</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
</pre></div>
</div>
<p>However, for the mesh command to work, we need the vectorized
extensions xv and yv of x and y.</p>
<p>FIGURE:[figs/mesh_ex1.eps] Result of the mesh command for plotting a 2D scalar field (Gnuplot backend). {fig:mesh_ex1}</p>
<p>The surf command employs the same syntax, but results in a different
plot (see Figure ref{fig:surf_ex1}):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">surf</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span>
</pre></div>
</div>
<p>FIGURE:[figs/surf_ex1.eps] Result of the surf command (Gnuplot backend). {fig:surf_ex1}</p>
<p>The surf command offers many possibilities to adjust the resulting plot:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">setp</span><span class="p">(</span><span class="n">interactive</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">surf</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span>
<span class="n">shading</span><span class="p">(</span><span class="s">&#39;flat&#39;</span><span class="p">)</span>
<span class="n">colorbar</span><span class="p">()</span>
<span class="n">colormap</span><span class="p">(</span><span class="n">hot</span><span class="p">())</span>
<span class="n">axis</span><span class="p">([</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="o">-</span><span class="mf">1.5</span><span class="p">,</span><span class="mf">1.5</span><span class="p">])</span>
<span class="n">view</span><span class="p">(</span><span class="mi">35</span><span class="p">,</span><span class="mi">45</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<p>Here we have specified a flat shading model, added a color bar, changed
the color map to hot, set some suitable axis values, and changed the
view point (the view takes two arguments: the azimuthal rotation and
the elevation, both given in degrees). 
The same plot can also be accomplished with one single, compound
statement (just as Easyviz offers for the plot command):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">surf</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span>
     <span class="n">shading</span><span class="o">=</span><span class="s">&#39;flat&#39;</span><span class="p">,</span>
     <span class="n">colorbar</span><span class="o">=</span><span class="s">&#39;on&#39;</span><span class="p">,</span>
     <span class="n">colormap</span><span class="o">=</span><span class="n">hot</span><span class="p">(),</span>
     <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="o">-</span><span class="mf">1.5</span><span class="p">,</span><span class="mf">1.5</span><span class="p">],</span>
     <span class="n">view</span><span class="o">=</span><span class="p">[</span><span class="mi">35</span><span class="p">,</span><span class="mi">45</span><span class="p">])</span>
</pre></div>
</div>
<p>Figure ref{fig:surf_ex2} displays the result.</p>
<p>FIGURE:[figs/surf_ex2.eps] Result of an extended surf command (Gnuplot backend). {fig:surf_ex2}</p>
</div>
<div class="section" id="contour-plots">
<h3>Contour Plots<a class="headerlink" href="#contour-plots" title="Permalink to this headline">¶</a></h3>
<p>A contour plot is another useful technique for visualizing scalar
fields. The primary examples on contour plots from everyday life is
the level curves on geographical maps, reflecting the height of the
terrain. Mathematically, a contour line, also called an isoline, is
defined as the implicit curve f(x,y)=c. The contour levels c are
normally uniformly distributed between the extreme values of the
function f (this is the case in a map: the height difference between
two contour lines is constant), but in scientific visualization it is
sometimes useful to use a few carefully selected c values to
illustrate particular features of a scalar field.</p>
<p>In Easyviz, there are several commands for creating different kinds of
contour plots:</p>
<blockquote>
<div><ul>
<li><p class="first">contour: Draw a standard contour plot, i.e., lines in the plane.</p>
</li>
<li><p class="first">contourf: Draw a filled 2D contour plot, where the space between
the contour lines is filled with colors.</p>
</li>
<li><p class="first">contour3: Same as contour, but the curves are drawn at their 
corresponding height levels in 3D space.</p>
</li>
<li><dl class="first docutils">
<dt>meshc: Works in the same way as mesh except that a</dt>
<dd><p class="first last">contour plot is drawn in the plane beneath the mesh.</p>
</dd>
</dl>
</li>
<li><p class="first">surfc: Same as meshc except that a solid surface is
drawn instead of a wireframe mesh.</p>
</li>
</ul>
</div></blockquote>
<p>We start with illustrating the plain contour command, assuming that
we already have computed the xv, yv, and values
arrays as shown in our first example on scalar field plotting.
The basic syntax follows that of mesh and surf:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">contour</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span>
</pre></div>
</div>
<p>By default, five uniformly spaced contour level curves are drawn, see
Figure ref{fig:contour_ex1}.</p>
<p>FIGURE:[figs/contour_ex1.eps] Result of the simplest possible contour command (Gnuplot backend). {fig:contour_ex1}</p>
<p>The number of levels in a contour plot can be specified with an additional
argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">n</span> <span class="o">=</span> <span class="mi">15</span>   <span class="c"># number of desired contour levels</span>
<span class="n">contour</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
</pre></div>
</div>
<p>The result can be seen in Figure ref{fig:contour_ex2}.</p>
<p>FIGURE:[figs/contour_ex2.eps] A contour plot with 15 contour levels (Gnuplot backend). {fig:contour_ex2}</p>
<p>Sometimes one wants contour levels that are not equidistant or not
distributed throughout the range of the scalar field. Individual
contour levels to be drawn can easily be specified as a list:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">levels</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">]</span>
<span class="n">contour</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">levels</span><span class="p">,</span> <span class="n">clabels</span><span class="o">=</span><span class="s">&#39;on&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Now, the levels list specify the values of the contour levels, and
the clabel keyword allows labeling of the level values in the plot.
Figure ref{fig:contour_ex3} shows the result. We remark that the
Gnuplot backend colors the contour lines and places the contour values
and corresponding colors beside the plot. Figures that are reproduced
in black and white only can then be hard to analyze. Other backends
may draw the contour lines in black and annotate each line with the
corresponding contour level value.  Such plots are better suited for
being displayed in black and white.</p>
<p>FIGURE:[figs/contour_ex3.eps] Four individually specified contour levels (Gnuplot backend). {fig:contour_ex3}</p>
<p>The contourf command:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">contourf</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span>
</pre></div>
</div>
<p>gives a filled contour plot as shown in Figure ref{fig:contourf_ex1}.
Only the Matplotlib and VTK backends currently supports filled
contour plots.</p>
<p>FIGURE:[figs/contourf_ex1.eps] Filled contour plot created by the contourf command (VTK backend). {fig:contourf_ex1}</p>
<p>The contour lines can be &#8220;lifted up&#8221; in 3D space, as shown in Figure
ref{fig:contour3_ex1}, using the contour3 command:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">contour3</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>
</pre></div>
</div>
<p>FIGURE:[figs/contour3_ex1.eps] Example on the contour3 command for elevated contour levels (Gnuplot backend). {fig:contour3_ex1}</p>
<p>Finally, we show a simple example illustrating the meshc and surfc
commands:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">meshc</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> 
      <span class="n">clevels</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> 
      <span class="n">colormap</span><span class="o">=</span><span class="n">hot</span><span class="p">(),</span> 
      <span class="n">grid</span><span class="o">=</span><span class="s">&#39;off&#39;</span><span class="p">)</span>
<span class="n">figure</span><span class="p">()</span>
<span class="n">surfc</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> 
      <span class="n">clevels</span><span class="o">=</span><span class="mi">15</span><span class="p">,</span> 
      <span class="n">colormap</span><span class="o">=</span><span class="n">hsv</span><span class="p">(),</span> 
      <span class="n">grid</span><span class="o">=</span><span class="s">&#39;off&#39;</span><span class="p">,</span>
      <span class="n">view</span><span class="o">=</span><span class="p">(</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">))</span>
</pre></div>
</div>
<p>The resulting plots are displayed in Figures ref{fig:meshc_ex1} and
ref{fig:surfc_ex1}.</p>
<p>FIGURE:[figs/meshc_ex1.eps] Wireframe mesh with contours at the bottom (Gnuplot backend). {fig:meshc_ex1}</p>
<p>FIGURE:[figs/surfc_ex1.eps] Surface plot with contours (Gnuplot backend). {fig:surfc_ex1}</p>
</div>
<div class="section" id="pseudocolor-plots">
<h3>Pseudocolor Plots<a class="headerlink" href="#pseudocolor-plots" title="Permalink to this headline">¶</a></h3>
<p>Another way of visualizing a 2D scalar field in Easyviz is the
pcolor command. This command creates a pseudocolor plot, which is a
flat surface viewed from above. The simplest form of this command
follows the syntax of the other commands:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">pcolor</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span>
</pre></div>
</div>
<p>We can set the color shading in a pseudocolor plot either by giving
the shading keyword argument to pcolor or by calling the shading
command. The color shading is specified by a string that can be either
&#8216;faceted&#8217; (default), &#8216;flat&#8217;, or &#8216;interp&#8217; (interpolated). The Gnuplot and
Matplotlib backends support &#8216;faceted&#8217; and &#8216;flat&#8217; only, while the
VTK backend supports all of them.</p>
<p>FIGURE:[figs/pcolor_ex1.eps] Pseudocolor plot (Gnuplot backend).</p>
</div>
<div class="section" id="isosurface-plots">
<h3>Isosurface Plots<a class="headerlink" href="#isosurface-plots" title="Permalink to this headline">¶</a></h3>
<p>For 3D scalar fields, isosurfaces or contour surfaces constitute the counterpart to contour
lines or isolines for 2D scalar fields. An isosurface connects points in
a scalar field with (approximately) the same scalar value and is
mathematically defined by the implicit equation f(x,y,z)=c. In Easyviz,
isosurfaces are created with the isosurface command. We will
demonstrate this command using 3D scalar field data from the flow
function. This function, also found in Matlab,
generates fluid flow data. Our first isosurface visualization example
then looks as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">flow</span><span class="p">()</span>  <span class="c"># generate fluid-flow data</span>
<span class="n">setp</span><span class="p">(</span><span class="n">interactive</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">h</span> <span class="o">=</span> <span class="n">isosurface</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span><span class="p">,</span><span class="n">v</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="n">h</span><span class="o">.</span><span class="n">setp</span><span class="p">(</span><span class="n">opacity</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
<span class="n">shading</span><span class="p">(</span><span class="s">&#39;interp&#39;</span><span class="p">)</span>
<span class="n">daspect</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">])</span>
<span class="n">view</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">axis</span><span class="p">(</span><span class="s">&#39;tight&#39;</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<p>After creating some scalar volume data with the flow function, we
create an isosurface with the isovalue -3. The isosurface is then
set a bit transparent (opacity=0.5) before we specify the shading
model and the view point. We also set the data aspect ratio to be
equal in all directions with the daspect command.  The resulting
plot is shown in Figure ref{fig:isosurface1}. We remark that the
Gnuplot backend does not support 3D scalar fields and hence not
isosurfaces.</p>
<p>FIGURE:[figs/isosurface1.eps] Isosurface plot (VTK backend). {fig:isosurface1}</p>
<p>Here is another example that demonstrates the isosurface command
(again using the flow function):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">flow</span><span class="p">()</span>
<span class="n">setp</span><span class="p">(</span><span class="n">interactive</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">h</span> <span class="o">=</span> <span class="n">isosurface</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span><span class="p">,</span><span class="n">v</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
<span class="n">shading</span><span class="p">(</span><span class="s">&#39;interp&#39;</span><span class="p">)</span>
<span class="n">daspect</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>
<span class="n">view</span><span class="p">([</span><span class="o">-</span><span class="mi">65</span><span class="p">,</span><span class="mi">20</span><span class="p">])</span>
<span class="n">axis</span><span class="p">(</span><span class="s">&#39;tight&#39;</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<p>Figure ref{fig:isosurface2} shows the resulting plot.</p>
<p>FIGURE:[figs/isosurface2.eps] Another isosurface plot (VTK backend). {fig:isosurface2}</p>
</div>
<div class="section" id="volumetric-slice-plot">
<h3>Volumetric Slice Plot<a class="headerlink" href="#volumetric-slice-plot" title="Permalink to this headline">¶</a></h3>
<p>Another way of visualizing scalar volume data is by using the <a href="#id7"><span class="problematic" id="id8">slice_</span></a>
command (since the name slice is already taken by a built-in
function in Python for array slicing, we have followed the standard
Python convention and added a trailing underscore to the name in
Easyviz - <a href="#id9"><span class="problematic" id="id10">slice_</span></a> is thus the counterpart to the Matlab function
slice.). This command draws orthogonal slice planes through a
given volumetric data set. Here is an example on how to use the
<a href="#id11"><span class="problematic" id="id12">slice_</span></a> command:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">ndgrid</span><span class="p">(</span><span class="n">seq</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="o">.</span><span class="mi">2</span><span class="p">),</span> <span class="n">seq</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="o">.</span><span class="mi">25</span><span class="p">),</span> <span class="n">seq</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="o">.</span><span class="mi">16</span><span class="p">),</span>
                   <span class="n">sparse</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">x</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="n">xslice</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mf">1.2</span><span class="p">,</span> <span class="o">.</span><span class="mi">8</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
<span class="n">yslice</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">zslice</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
<span class="n">slice_</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">xslice</span><span class="p">,</span> <span class="n">yslice</span><span class="p">,</span> <span class="n">zslice</span><span class="p">,</span>
       <span class="n">colormap</span><span class="o">=</span><span class="n">hsv</span><span class="p">(),</span> <span class="n">grid</span><span class="o">=</span><span class="s">&#39;off&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that we here use the SciTools function seq for specifying a
uniform partitioning of an interval - the linspace function from
numpy could equally well be used.  The first three arguments in the
<a href="#id13"><span class="problematic" id="id14">slice_</span></a> call are the grid points in the x, y, and z
directions. The fourth argument is the scalar field defined on-top of
the grid. The next three arguments defines either slice planes in the
three space directions or a surface plane (currently not working). In
this example we have created 6 slice planes: Three at the x axis (at
x=-1.2, x=0.8, and x=2), one at the y axis (at y=2), and two
at the z axis (at z=-2 and z=0.0). The result is presented in
Figure ref{fig:slice1}.</p>
<p>FIGURE:[figs/slice1.eps] Slice plot where the x axis is sliced at -1.2, 0.8, and 2, the y axis is sliced at 2, and the z axis is sliced at -2 and 0.0 (VTK backend). {fig:slice1}</p>
<p><em>Contours in Slice Planes.</em> With the contourslice command we can create contour plots
in planes aligned with the coordinate axes. Here is an example 
using 3D scalar field data from the flow function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">flow</span><span class="p">()</span>
<span class="n">setp</span><span class="p">(</span><span class="n">interactive</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">h</span> <span class="o">=</span> <span class="n">contourslice</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">seq</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">9</span><span class="p">),</span> <span class="p">[],</span> <span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">8</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">10</span><span class="p">))</span>
<span class="n">axis</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="n">daspect</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
<span class="n">ax</span> <span class="o">=</span> <span class="n">gca</span><span class="p">()</span>
<span class="n">ax</span><span class="o">.</span><span class="n">setp</span><span class="p">(</span><span class="n">fgcolor</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span> <span class="n">bgcolor</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">))</span>
<span class="n">box</span><span class="p">(</span><span class="s">&#39;on&#39;</span><span class="p">)</span>
<span class="n">view</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<p>The first four arguments given to contourslice in this example are
the extended coordinates of the grid (x, y, z) and the 3D scalar
field values in the volume (v). The next three arguments defines the
slice planes in which we want to draw contour lines. In this
particular example we have specified two contour plots in the planes
x=1,2,dots,9, none in y=const planes (empty
list) , and one contour plot in the plane z=0. The last argument to
contourslice is optional, it can be either an integer specifying the
number of contour lines (the default is five) or, as in the current
example, a list specifying the level curves. Running the set of
commands results in the plot shown in Figure ref{fig:contourslice1}.</p>
<p>FIGURE:[figs/contourslice1.eps] Contours in slice planes (VTK backend). {fig:contourslice1}</p>
<p>Here is another example where we draw contour slices from a
three-dimensional MRI data set:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">scipy.io</span>
<span class="n">mri</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">io</span><span class="o">.</span><span class="n">loadmat</span><span class="p">(</span><span class="s">&#39;mri_matlab_v6.mat&#39;</span><span class="p">)</span>
<span class="n">D</span> <span class="o">=</span> <span class="n">mri</span><span class="p">[</span><span class="s">&#39;D&#39;</span><span class="p">]</span>
<span class="n">image_num</span> <span class="o">=</span> <span class="mi">8</span>

<span class="c"># Displaying a 2D Contour Slice</span>
<span class="n">contourslice</span><span class="p">(</span><span class="n">D</span><span class="p">,</span> <span class="p">[],</span> <span class="p">[],</span> <span class="n">image_num</span><span class="p">,</span> <span class="n">daspect</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span> <span class="n">indexing</span><span class="o">=</span><span class="s">&#39;xy&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The MRI data set is loaded from the file mri_matlab_v6.mat with the
aid from the loadmat function available in the io module in the
SciPy package. We then create a 2D contour slice plot with one slice
in the plane z=8. Figure ref{fig:contourslice3} displays the result.</p>
<p>FIGURE:[figs/contourslice3.eps] Contour slice plot of a 3D MRI data set (VTK backend). {fig:contourslice3}</p>
</div>
</div>
<div class="section" id="visualization-of-vector-fields">
<h2>Visualization of Vector Fields<a class="headerlink" href="#visualization-of-vector-fields" title="Permalink to this headline">¶</a></h2>
<p>A vector field is a function from space or space-time to a vector
value, where the number of components in the vector corresponds to
the number of space dimensions. Primary examples on vector fields
are the gradient of a scalar field; or velocity, displacement, or
force in continuum physics.</p>
<p>In Easyviz, a vector field can be visualized either by a quiver
(arrow) plot or by various kinds of stream plots like stream lines,
stream ribbons, and stream tubes. Below we will look closer at each of
these visualization techniques.</p>
<div class="section" id="quiver-plots">
<h3>Quiver Plots<a class="headerlink" href="#quiver-plots" title="Permalink to this headline">¶</a></h3>
<p>The quiver and quiver3 commands draw arrows to illustrate vector
values (length and direction) at discrete points.  As the names
indicate, quiver is for 2D vector fields in the plane and quiver3
plots vectors in 3D space.  The basic usage of the quiver command
goes as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">21</span><span class="p">)</span>
<span class="n">xv</span><span class="p">,</span> <span class="n">yv</span> <span class="o">=</span> <span class="n">ndgrid</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">sparse</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">values</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">xv</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">yv</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="n">uv</span><span class="p">,</span> <span class="n">vv</span> <span class="o">=</span> <span class="n">gradient</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
<span class="n">quiver</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">uv</span><span class="p">,</span> <span class="n">vv</span><span class="p">)</span>
</pre></div>
</div>
<p>Our vector field in this example is simply the gradient of the scalar
field used to illustrate the commands for 2D scalar field plotting.
The gradient function computes the gradient using finite difference
approximations.  The result is a vector field with components uv and
vv in the x and y directions, respectively.  The grid points and
the vector components are passed as arguments to quiver, which in
turn produces the plot in Figure ref{fig:quiver_ex1}.</p>
<p>FIGURE:[figs/quiver_ex1.eps] Velocity vector plot (Gnuplot backend). {fig:quiver_ex1}</p>
<p>The arrows in a quiver plot are automatically scaled to fit within the
grid. If we want to control the length of the arrows, we can pass an
additional argument to scale the default lengths:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">scale</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">quiver</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">uv</span><span class="p">,</span> <span class="n">vv</span><span class="p">,</span> <span class="n">scale</span><span class="p">)</span>
</pre></div>
</div>
<p>This value of scale will thus stretch the vectors to their double length. 
To turn off the automatic scaling, we can set the scale value to zero.</p>
<p>Quiver plots are often used in combination with other plotting
commands such as pseudocolor plots or contour plots, since this may
help to get a better perception of a given set of data. Here is an
example demonstrating this principle for a simple scalar field, where
we plot the field values as colors and add vectors to illustrate the
associated gradient field:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span> <span class="o">=</span> <span class="n">ndgrid</span><span class="p">(</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">101</span><span class="p">),</span> <span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">101</span><span class="p">))</span>
<span class="n">values</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">xv</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">yv</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="n">pcolor</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">shading</span><span class="o">=</span><span class="s">&#39;interp&#39;</span><span class="p">)</span>

<span class="c"># Create a coarser grid for the gradient field</span>
<span class="n">xv</span><span class="p">,</span> <span class="n">yv</span> <span class="o">=</span> <span class="n">ndgrid</span><span class="p">(</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">21</span><span class="p">),</span> <span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">21</span><span class="p">))</span>
<span class="n">values</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">xv</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">yv</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="n">uv</span><span class="p">,</span> <span class="n">vv</span> <span class="o">=</span> <span class="n">gradient</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
<span class="n">hold</span><span class="p">(</span><span class="s">&#39;on&#39;</span><span class="p">)</span>
<span class="n">quiver</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">uv</span><span class="p">,</span> <span class="n">vv</span><span class="p">,</span> <span class="s">&#39;filled&#39;</span><span class="p">,</span> <span class="s">&#39;k&#39;</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">])</span>
<span class="n">figure</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">contour</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span> 
<span class="n">hold</span><span class="p">(</span><span class="s">&#39;on&#39;</span><span class="p">)</span>
<span class="n">quiver</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">uv</span><span class="p">,</span> <span class="n">vv</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">])</span> 
</pre></div>
</div>
<p>The resulting plots can be seen in Figure ref{fig:quiver_ex2} and
ref{fig:quiver_ex3}.</p>
<p>FIGURE:[figs/quiver_ex2.eps] Combined quiver and pseudocolor plot (VTK backend). {fig:quiver_ex2}
FIGURE:[figs/quiver_ex3.eps] Combined quiver and pseudocolor plot (VTK backend). {fig:quiver_ex3}</p>
<p>Visualization of 3D vector fields by arrows at grid points can be done
with the quiver3 command. At the time of this writing, only the VTK
backend supports 3D quiver plots. A simple example of plotting the
&#8220;radius vector field&#8221; v=(x,y,z) is given next:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">=</span> <span class="n">z</span> <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>
<span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">zv</span> <span class="o">=</span> <span class="n">ndgrid</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">sparse</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">uv</span> <span class="o">=</span> <span class="n">xv</span>
<span class="n">vv</span> <span class="o">=</span> <span class="n">yv</span>
<span class="n">wv</span> <span class="o">=</span> <span class="n">zv</span>
<span class="n">quiver3</span><span class="p">(</span><span class="n">xv</span><span class="p">,</span> <span class="n">yv</span><span class="p">,</span> <span class="n">zv</span><span class="p">,</span> <span class="n">uv</span><span class="p">,</span> <span class="n">vv</span><span class="p">,</span> <span class="n">wv</span><span class="p">,</span> <span class="s">&#39;filled&#39;</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="o">-</span><span class="mi">7</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="o">-</span><span class="mi">7</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="o">-</span><span class="mi">7</span><span class="p">,</span><span class="mi">7</span><span class="p">])</span>
</pre></div>
</div>
<p>The strings &#8216;filled&#8217; and &#8216;r&#8217; are optional and makes the arrows
become filled 
and red, respectively. The resulting plot is presented in Figure 
ref{fig:quiver3_ex1}.</p>
<p>FIGURE:[figs/quiver3_ex1.eps] 3D quiver plot (VTK backend). {fig:quiver3_ex1}</p>
</div>
<div class="section" id="stream-plots">
<h3>Stream Plots<a class="headerlink" href="#stream-plots" title="Permalink to this headline">¶</a></h3>
<p>Stream plots constitute an alternative to arrow plots for visualizing
vector fields.  The stream plot commands currently available in
Easyviz are streamline, streamtube, and streamribbon.  Stream
lines are lines aligned with the vector field, i.e., the vectors are
tangents to the streamlines. Stream tubes are similar, but now the
surfaces of thin tubes are aligned with the vectors.  Stream ribbons
are also similar: thin sheets are aligned with the vectors. The latter
type of visualization is also known as stream or flow sheets.  In the
near future, Matlab commands such as streamslice and
streamparticles might also be implemented.</p>
<p>We start with an example on how to use the streamline command. In
this example (and in the following examples) we will use the wind
data set that is included with Matlab. This data set represents air
currents over a region of North America and is suitable for testing
the different stream plot commands. The following commands will load
the wind data set and then draw some stream lines from it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">scipy.io</span>  <span class="c"># needed to load binary .mat-files</span>

<span class="c"># Load the wind data set and create variables</span>
<span class="n">wind</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">io</span><span class="o">.</span><span class="n">loadmat</span><span class="p">(</span><span class="s">&#39;wind.mat&#39;</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">wind</span><span class="p">[</span><span class="s">&#39;x&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">wind</span><span class="p">[</span><span class="s">&#39;y&#39;</span><span class="p">]</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">wind</span><span class="p">[</span><span class="s">&#39;z&#39;</span><span class="p">]</span>
<span class="n">u</span> <span class="o">=</span> <span class="n">wind</span><span class="p">[</span><span class="s">&#39;u&#39;</span><span class="p">]</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">wind</span><span class="p">[</span><span class="s">&#39;v&#39;</span><span class="p">]</span>
<span class="n">w</span> <span class="o">=</span> <span class="n">wind</span><span class="p">[</span><span class="s">&#39;w&#39;</span><span class="p">]</span>

<span class="c"># Create starting points for the stream lines</span>
<span class="n">sx</span><span class="p">,</span> <span class="n">sy</span><span class="p">,</span> <span class="n">sz</span> <span class="o">=</span> <span class="n">ndgrid</span><span class="p">([</span><span class="mi">80</span><span class="p">]</span><span class="o">*</span><span class="mi">4</span><span class="p">,</span> <span class="n">seq</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="mi">10</span><span class="p">),</span> <span class="n">seq</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">15</span><span class="p">,</span><span class="mi">5</span><span class="p">),</span> 
                    <span class="n">sparse</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
  
<span class="c"># Draw stream lines</span>
<span class="n">streamline</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">,</span> <span class="n">sx</span><span class="p">,</span> <span class="n">sy</span><span class="p">,</span> <span class="n">sz</span><span class="p">,</span>
           <span class="n">view</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="mi">60</span><span class="p">,</span><span class="mi">140</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">60</span><span class="p">,</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="mi">20</span><span class="p">])</span>
</pre></div>
</div>
<p>The wind data set is stored in a binary <cite>.mat</cite>-file called
wind.mat. To load the data in this file into Python, we can use the
loadmat function which is available through the io module in
SciPy. Using the loadmat function on the <cite>wind.mat</cite>-file returns a
Python dictionary (called wind in the current example) containing the NumPy
arrays x, y, z, u, v, and w. The arrays u, v, and w
are the 3D vector data, while the arrays x, y, and z defines the
(3D extended) coordinates for the associated grid. The data arrays in
the dictionary wind are then stored in seperate variables for easier
access later.</p>
<p>Before we call the streamline command we must set up some starting
point coordinates for the stream lines. In this example, we have used
the ndgrid command to define the starting points with the line:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">sx</span><span class="p">,</span> <span class="n">sy</span><span class="p">,</span> <span class="n">sz</span> <span class="o">=</span> <span class="n">ndgrid</span><span class="p">([</span><span class="mi">80</span><span class="p">]</span><span class="o">*</span><span class="mi">4</span><span class="p">,</span> <span class="n">seq</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="mi">10</span><span class="p">),</span> <span class="n">seq</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">15</span><span class="p">,</span><span class="mi">5</span><span class="p">))</span>
</pre></div>
</div>
<p>This command defines starting points which all lie on x=80,
y=20,30,40,50, and z=0,5,10,15. We now have all the data we need
for calling the streamline command. The first six arguments to the
streamline command are the grid coordinates (x,y,z) and the 3D
vector data (u,v,w), while the next three arguments are the starting
points which we defined with the ndgrid command above. The
resulting plot is presented in Figure ref{fig:streamline_ex1}.</p>
<p>FIGURE:[figs/streamline_ex1.eps] Stream line plot (Vtk backend). {fig:streamline_ex1}</p>
<p>The next example demonstrates the streamtube command applied to the 
same wind data set:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">streamtube</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">,</span> <span class="n">sx</span><span class="p">,</span> <span class="n">sy</span><span class="p">,</span> <span class="n">sz</span><span class="p">,</span>
           <span class="n">daspect</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span>
           <span class="n">view</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
           <span class="n">axis</span><span class="o">=</span><span class="s">&#39;tight&#39;</span><span class="p">,</span>
           <span class="n">shading</span><span class="o">=</span><span class="s">&#39;interp&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The arrays sx, sy, and sz are the same as in the previous
example and defines the starting positions for the center lines of the
tubes. The resulting plot is presented in Figure
ref{fig:streamtube_ex1}.</p>
<p>FIGURE:[figs/streamtube_ex1.eps] Stream tubes (Vtk backend). {fig:streamtube_ex1}</p>
<p>Finally, we illustrate the streamribbon command:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">streamribbon</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">,</span> <span class="n">sx</span><span class="p">,</span> <span class="n">sy</span><span class="p">,</span> <span class="n">sz</span><span class="p">,</span>
             <span class="n">ribbonwidth</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>
             <span class="n">daspect</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span>
             <span class="n">view</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
             <span class="n">axis</span><span class="o">=</span><span class="s">&#39;tight&#39;</span><span class="p">,</span>
             <span class="n">shading</span><span class="o">=</span><span class="s">&#39;interp&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Figure ref{fig:streamribbon_ex1} shows the resulting stream ribbons.</p>
<p>FIGURE:[figs/streamribbon_ex1.eps] Stream ribbons (VTK backend). {fig:streamribbon_ex1}</p>
</div>
<div class="section" id="bar-charts">
<h3>Bar Charts<a class="headerlink" href="#bar-charts" title="Permalink to this headline">¶</a></h3>
<p>Easyviz also supports a unified interface to simple bar charts.
Here is a simple example for displaying tabular values, with one
bar for each data point:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>
<span class="n">languages</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;C&#39;</span><span class="p">,</span> <span class="s">&#39;Java&#39;</span><span class="p">,</span> <span class="s">&#39;C++&#39;</span><span class="p">,</span> <span class="s">&#39;PHP&#39;</span><span class="p">,</span> <span class="s">&#39;VB&#39;</span><span class="p">,</span> <span class="s">&#39;C#&#39;</span><span class="p">,</span> <span class="s">&#39;Python&#39;</span><span class="p">,</span> 
             <span class="s">&#39;Perl&#39;</span><span class="p">,</span> <span class="s">&#39;JavaScript&#39;</span><span class="p">]</span>
<span class="n">ratings</span> <span class="o">=</span> <span class="p">[</span><span class="mi">18</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mf">9.7</span><span class="p">,</span> <span class="mf">9.7</span><span class="p">,</span> <span class="mf">6.4</span><span class="p">,</span> <span class="mf">4.4</span><span class="p">,</span> <span class="mf">4.2</span><span class="p">,</span> <span class="mf">3.6</span><span class="p">,</span> <span class="mf">2.5</span><span class="p">]</span>
<span class="n">bar</span><span class="p">(</span><span class="n">ratings</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">,</span>
    <span class="n">barticks</span><span class="o">=</span><span class="n">languages</span><span class="p">,</span>
    <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;Ratings in percent (TIOBE Index, April 2010)&#39;</span><span class="p">,</span>
    <span class="n">axis</span><span class="o">=</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">languages</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">20</span><span class="p">],</span>
    <span class="n">hardcopy</span><span class="o">=</span><span class="s">&#39;tmp.eps&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The bar chart illustrates the data in the ratings list. These data
correspond to the names in languages.</p>
<p>FIGURE:[figs/pyranking.eps] A simple bar chart illustrating the popularity of common programming languages.</p>
<p>One may display groups of bars. The data can then be put in a matrix,
where rows (1st index) correspond to the groups the columns to the
data within one group:</p>
<div class="highlight-python"><pre>data = [[ 0.15416284  0.7400497   0.26331502]
        [ 0.53373939  0.01457496  0.91874701]
        [ 0.90071485  0.03342143  0.95694934]
        [ 0.13720932  0.28382835  0.60608318]]
bar(data, 
    barticks=['group 1', 'group 2', 'group 3', 'group 4'],
    legend=['bar 1', 'bar 2', 'bar 3'],
    axis=[-1, data.shape[0], 0, 1.3],
    ylabel='Normalized CPU time',
    title='Bars from a matrix, now with more annotations')</pre>
</div>
<p>When the names of the groups (barticks) are quite long, rotating them
90 degrees is preferable, and this is done by the keyword
argument rotated_barticks=True.</p>
<p>The demo program in examples/bar_demo.py contains additional examples
and features.</p>
</div>
</div>
<div class="section" id="backends">
<h2>Backends<a class="headerlink" href="#backends" title="Permalink to this headline">¶</a></h2>
<p>As we have mentioned earlier, Easyviz is just a unified interface to
other plotting packages, which we refer to as backends. We have
currently implemented backends for Gnuplot, Grace, OpenDX, Matlab,
Matplotlib, Pmw.Blt, Veusz, VisIt, and VTK. Some are more early in
developement than others, like the backends for OpenDx and VisIt.</p>
<p>Because of limitations in many of the plotting packages, not all
features in Easyviz are supported by each of the backends.  Gnuplot
has (at the time of this writing) no support for visualization of 3D
vector fields, so this is of course not available in the Gnuplot
backend either.</p>
<p>Some supported visualization programs are commented on below.</p>
<p><em>Gnuplot.</em> Gnuplot is a command-driven interactive or scripted
plotting utility that works on a wide variety of platforms. Gnuplot
supports many types of plots in both 2D and 3D, including curve plots,
contour plots, vector plots, and surface plots.  3D scalar and vector
fields are not supported. To access Gnuplot from Python and send NumPy
arrays to Gnuplot, we use the Python module Gnuplot.</p>
<p><em>Matlab.</em> Many view Matlab as the de facto standard for making curves
and plots of 2D scalar/vector fields.</p>
<p><em>Matplotlib.</em> Matplotlib is now quickly gaining wide popularity in
the scientific Python community and has established itself as the de
facto standard for curve plotting and 2D contour and (recently) surface
plotting. The interface to Matplotlib is Matlab-insipired, and
different backends are used to create the plots: Gtk, Tk, WxWidgets
and many more.  (Since Easyviz and Matplotlib haver very similar
Matlab-style syntax, Easyviz is just a thin layer on top of Matplotlib
to enable Matplotlib to be used with the Easyviz unified syntax.)
Matplotlib is now a comprehensive package with lots of tuning
possibilities that Easyviz does not support - but one can fetch the
underlying Matplotlib from Easyviz and call all the functionality of
Matplotlib directly.</p>
<p><em>Grace.</em> Grace is a highly interactive curve plotting program on the
Unix/X11 platform which has been popular for many years. It does not
support 2D or 3D scalar or vector fields. However, it has a lot of
functionality for computing with curves and adjusting/fine-tuning
plots interactively.</p>
<p><em>PyX.</em> PyX is a Python package for the creation of PostScript and
PDF files. It combines an abstraction of the PostScript drawing model
with a TeX/LaTeX interface. Complex tasks like 2d and 3d plots in
publication-ready quality are built out of these primitives.</p>
<p><em>Pmw.Blt.Graph.</em> Pmw (Python Mega Widgets) extends the Tkinter
package with more sophisticated widgets, included an interactive
widget for curve plotting. This widget is based on the BLT package
(an extension of Tk written in C).
The BLT backend offers currenlty only basic plotting functionality.</p>
<p><em>Veusz.</em> From Veusz homepage (<a class="reference external" href="http://home.gna.org/veusz">http://home.gna.org/veusz</a>): Veusz is a
GUI scientific plotting and graphing package. It is designed to
produce publication-ready Postscript or PDF output. SVG, EMF and
bitmap formats export are also supported. Veusz has a comprehensive
GUI and produces really high-quality plots.</p>
<p><em>VTK.</em> VTK (Visualization ToolKit) is a package primarily aimed at
visualizing 2D and 3D scalar and vector fields by a range of techniques.
VTK is used to achieve 2D and 3D visualizations of the same type as 
Matlab offers. However, VTK can do much more (although the Easyviz 
commands are restricted to what is typically offered by Matlab).</p>
</div>
<div class="section" id="design">
<h2>Design<a class="headerlink" href="#design" title="Permalink to this headline">¶</a></h2>
<div class="section" id="main-objects">
<h3>Main Objects<a class="headerlink" href="#main-objects" title="Permalink to this headline">¶</a></h3>
<p>All code that is common to all backends is gathered together in a file
called common.py. For each backend there is a separate file where
the backend dependent code is stored. For example, code that are
specific for the Gnuplot backend, are stored in a file called
<a class="reference internal" href="#gnuplot">gnuplot</a>.py and code specific for the VTK backend are stored in
<a href="#id15"><span class="problematic" id="id16">vtk_</span></a>.py (note the final underscore in the stem of the filename - all
backend files have this underscore).</p>
<p>Each backend is a subclass of class BaseClass. The BaseClass code
is found in common.py and contains all common code for the backends.
Basically, a backend class extends BaseClass with
rendering capabilities and backend-specific functionality.</p>
<p>The most important method that needs to be implemented in the backend
is the _replot method, which updates the backend and the plot after a
change in the data. Another important method for the backend class is
the hardcopy method, which stores an image of the data in the current
figure to a file.</p>
<p>Inspired by Matlab, the Easyviz interface is organized around figures and
axes. A figure contains an arbitrary number of axes, and the axes can
be placed in arbitrary positions in the figure window. Each figure appears
in a separate window on the screen. The current figure is accessed by
the gcf() call. Similarly, the current axes are accessed by calling
gca().</p>
<p>It is
natural to have one class for figures and one for axes. Class Figure
contains a dictionary with one (default) or more Axis objects in
addition to several properties such as figure width and height. Class Axis
has another dictionary with the plot data as well as lots of
parameters for colors, text fonts, labels on the axes, hidden surfaces, etc.
For example, when adding an
elevated surface to the current figure, this surface will be
appended to a list in the current Axis object. 
Optionally one can add the surface to another Axis
object by specifying the Axis instance as an argument.</p>
<p>All the objects that are to be plotted in a figure such as curves,
surfaces, vectors, and so on, are stored in repsectively classes.  An
elevated surface, for instance, is represented as an instance of class
Surface.  All such classes are subclasses of
PlotProperties. Besides being the base class of all objects that can
be plotted in a figure
(Line, 
Surface, 
Contours, 
VelocityVectors, 
Streams, 
Volume), 
class PlotProperties also stores various properties that are common
to all objects in a figure. Examples include line properties, material
properties, storage arrays for x and y values for Line objects,
and x, y, and z values for 3D objects such as Volume.</p>
<p>The classes mentioned above, i.e., BaseClass with subclasses, class
PlotProperties with subclasses, as well as class Figure and class
Axis constitute the most important classes in the Easyviz interface.
Other less important classes are Camera, Light, Colorbar, and
MaterialProperties.</p>
<p>All the classes in common.py follows a convention where class parameters
are set by a setp method and read by a getp method. For
example, we can set the limits on the x axis by using the setp
method in a Axis instance:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ax</span> <span class="o">=</span> <span class="n">gca</span><span class="p">()</span>                  <span class="c"># get current axis</span>
<span class="n">ax</span><span class="o">.</span><span class="n">setp</span><span class="p">(</span><span class="n">xmin</span><span class="o">=-</span><span class="mi">2</span><span class="p">,</span> <span class="n">xmax</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>To extract the values of these limits we can write:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">xmin</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">getp</span><span class="p">(</span><span class="s">&#39;xmin&#39;</span><span class="p">)</span>
<span class="n">xmax</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">getp</span><span class="p">(</span><span class="s">&#39;xmax&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Normal use will seldom involve setp and getp functions, since most
users will apply the Matlab-inspired interface and set, e.g., the
limits by:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">xlim</span><span class="p">([</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">])</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="installation">
<h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>Easyviz comes with the SciTools package, so to install Easyviz, you
must install SciTools, which is available from
Google code (<a class="reference external" href="http://code.google.com/p/scitools">http://code.google.com/p/scitools</a>).</p>
<p>If you run a Linux system that allows installation from Debian
repositories (Ubuntu is an example), you get SciTools and all its
dependencies with one Unix command:</p>
<div class="highlight-python"><pre>Unix&gt; sudo apt-get install python-scitools</pre>
</div>
<p>(SciTools is in standard Debian.)</p>
<p>Otherwise, you download the tarball with the SciTools software, pack it out,
go the scitools folder, and run the standard command:</p>
<div class="highlight-python"><pre>Unix/DOS&gt; python setup.py install</pre>
</div>
<p>Easyviz is reached as the package scitools.easyviz and can be
imported in several ways (see the paragraph heading
&#8220;Importing Just Easyviz&#8221; in the Tutorial).</p>
<p>Easyviz will not work unless you have one or more plotting programs
correctly installed. Below, we have collected some brief information
on installing various programs. (Note that if you do an apt-get
install python-scitools all necessary plotting programs are
automatically installed for you.)</p>
<p>Please check your plotting program independently of Easyviz, as
described in the <em>Check Your Backends!</em> section of the <em>Troubleshooting</em>
chapter, if you encounter strange errors during Easyviz plotting.</p>
<div class="section" id="installing-gnuplot">
<h3>Installing Gnuplot<a class="headerlink" href="#installing-gnuplot" title="Permalink to this headline">¶</a></h3>
</div>
<div class="section" id="linux-unix">
<h3>Linux/Unix<a class="headerlink" href="#linux-unix" title="Permalink to this headline">¶</a></h3>
<p><em>Compile from Source.</em> Gnuplot can be downloaded from gnuplot.sourceforge.net. It builds 
easily on most Unix systems. You also need the Gnuplot Python
module, which can be obtained from gnuplot-py.sourceforge.net.</p>
<p><em>Debian/Ubuntu.</em> Prebuilt versions are available for Debian/Ubuntu:
run:</p>
<div class="highlight-python"><pre>apt-get install gnuplot gnuplot-x11 python-gnuplot</pre>
</div>
<p>but running these commands are not necessary since on Debian/Ubuntu you
will install python-scitools which effectively installs all the
software that SciTools depend on.</p>
</div>
<div class="section" id="windows">
<h3>Windows<a class="headerlink" href="#windows" title="Permalink to this headline">¶</a></h3>
<p>On Windows, one can either use Gnuplot under Cygwin or use a precompiled
binary from sourgeforce.net.</p>
<p><em>Using the Gnuplot Cygwin package.</em> In this case there are two things that needs to be changed in the
gp_cygwin.py file in the top-level directory of the Gnuplot.py
source tree. First you need to change the gnuplot_command variable
to gnuplot instead of pgnuplot.exe. Then you should change the
default_term variable to x11 instead of windows since the
Gnuplot Cygwin package is not compiled with the Windows
terminal. Finally, install Gnuplot.py (python setup.py install)
and launch X11 by running startx from a Cygwin prompt. Try to run
the test.py script that comes with Gnuplot.py. If everything
works, Easyviz can use Gnuplot.</p>
<p><em>Using Gnuplot Binaries.</em> First download the Gnuplot 4.2.4 binaries for Windows (or a newer version)
A possible URL is:</p>
<div class="highlight-python"><pre>http://prdownloads.sourceforge.net/sourceforge/gnuplot/gp424win32.zip</pre>
</div>
<p>The zip file may have another name for a newer version of Gnuplot on
Windows.</p>
<p>Then unzip the gp424win32.zip file to the folder:</p>
<div class="highlight-python"><pre>C:\gnuplot</pre>
</div>
<p>Add the folder name:</p>
<div class="highlight-python"><pre>C:\gnuplotin</pre>
</div>
<p>to the PATH environment variable (this is done in a graphical interface for
setting environment variables).</p>
<p>Check out the latest SVN revision of the Python interface to
Gnuplot, which is the Python module file Gnuplot.py:</p>
<div class="highlight-python"><pre>svn co https://gnuplot-py.svn.sourceforge.net/svnroot/gnuplot-py/trunk/gnuplot-py</pre>
</div>
<p>Install Gnuplot.py:</p>
<div class="highlight-python"><pre>cd gnuplot-py
python setup.py bdist_wininst
dist\gnuplot-py-1.8+.win32.exe</pre>
</div>
<p>Check out the latest SVN revision of SciTools:</p>
<div class="highlight-python"><pre>svn co http://scitools.googlecode.com/svn/trunk/ scitools</pre>
</div>
<p>Install SciTools:</p>
<div class="highlight-python"><pre>cd scitools
python setup.py bdist_wininst
dist\SciTools-0.4.win32.exe</pre>
</div>
<p>(The SciTools version number differs.)</p>
</div>
<div class="section" id="installing-matplotlib">
<h3>Installing Matplotlib<a class="headerlink" href="#installing-matplotlib" title="Permalink to this headline">¶</a></h3>
<p>This is normally just a matter of:</p>
<div class="highlight-python"><pre>python setup.py install</pre>
</div>
<p>in the root directory of the Matplotlib code.</p>
<p><em>Windows.</em> You can download prebuilt binaries from the Matplotlib home page.</p>
</div>
</div>
<div class="section" id="troubleshooting">
<h2>Troubleshooting<a class="headerlink" href="#troubleshooting" title="Permalink to this headline">¶</a></h2>
<div class="section" id="can-i-perform-a-diagnostic-test-of-easyviz">
<h3>Can I Perform a Diagnostic Test of Easyviz?<a class="headerlink" href="#can-i-perform-a-diagnostic-test-of-easyviz" title="Permalink to this headline">¶</a></h3>
<p>Yes. It is wise to perform a diagnostic test before reporting any error
or trouble to the SciTools maintainers. Find the source folder of SciTools
and go to the misc subfolder. Run:</p>
<div class="highlight-python"><pre>python diagonstic.py</pre>
</div>
<p>On the screen, you can see what you have of working software that Easyviz
may use. You do not need to see &#8220;ok&#8221; after each test, but at least
one plotting program must be properly installed. Include the detailed
diagonstics in the scitools_diagnostic.log file as attachment in any
mail to the SciTools developers.</p>
</div>
<div class="section" id="the-plot-window-disappears-immediately">
<h3>The Plot Window Disappears Immediately<a class="headerlink" href="#the-plot-window-disappears-immediately" title="Permalink to this headline">¶</a></h3>
<p>Depending on the backend used for plotting with Easyviz, the plot
window may be killed when the program terminates. Adding a statement
that makes the program halt provides a remedy:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nb">raw_input</span><span class="p">(</span><span class="s">&#39;Press Return key to quit: &#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The plot window will now stay on the screen until hitting the Enter/Return key.</p>
<p>Another remedy can be to add a show() call at the end of the plotting:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="i-get-thread-errors-while-plotting">
<h3>I Get Thread Errors While Plotting<a class="headerlink" href="#i-get-thread-errors-while-plotting" title="Permalink to this headline">¶</a></h3>
<p>With the Gnuplot backend, thread errors from Python may occur if you
plot many curves. The remedy is to do import time and insert
a time.sleep(0.2) (pause the program for 0.2 sec) between each call
to the plot command.</p>
<p>Remark: Scitools v0.8 automatically inserts a 0.2 sec pause when
plotting many curves with the Gnuplot backend.</p>
</div>
<div class="section" id="i-get-strange-errors-saying-something-about-latex">
<h3>I Get Strange Errors Saying Something About LaTeX<a class="headerlink" href="#i-get-strange-errors-saying-something-about-latex" title="Permalink to this headline">¶</a></h3>
<p>You probably run Easyviz with Matplotlib as backend, and you do not
have a working LaTeX installation. Matplotlib applies LaTeX for
improved rendering of legends, titles, and numbers.  The fix is to
turn off the use of LaTeX, which is done by the text.usetex
parameter in the matplotlib section of the configuration file.  Set
this parameter to false. See the subsection &#8220;Setting Parameters in
the Configuration File&#8221; in the section &#8220;Advanced Easyviz Topics&#8221; in
the Easyviz tutorial. The tutorial can be reached from the code.google.com
site or by running pydoc scitools.easyviz. If you use Matplotlib as
default plotting engine, we recommend to have a .scitools.cfg
configuration file in your home folder and that use control the use
of Matplotlib parameters in this file.</p>
<p>Another fix of LaTeX-related problems is to switch to another backend
than Matplotlib.</p>
</div>
<div class="section" id="old-programs-with-2d-scalar-vector-field-plotting-do-not-work">
<h3>Old Programs with 2D Scalar/Vector Field Plotting Do Not Work<a class="headerlink" href="#old-programs-with-2d-scalar-vector-field-plotting-do-not-work" title="Permalink to this headline">¶</a></h3>
<p>SciTools version 0.7 changed the default backend for plotting to
Matplotlib instead of Gnuplot (provided you have Matplotlib and you
run setup.py to install SciTools - binaries for Debian still has
Gnuplot as the plotting engine). Some functionality in Gnuplot, especially
regarding 2D vector/scalar fields, is not yet present in Matplotlib
and/or supported by the Easyviz interface to Matplotlib.
You then need to explicitly run the script with Gnuplot as plottin
engine:</p>
<div class="highlight-python"><pre>python myprogram.py --SCITOOLS_easyviz_backend gnuplot</pre>
</div>
<p>or you must import gnuplot explicitly in the program:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">scitools.easyviz.gnuplot_</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>or you can edit the installed scitools.cfg file (&#8220;backend&#8221; keyword
in the &#8220;easyviz&#8221; section), or your local version .scitools.cfg in
your home folder, or maybe the simplest solution is to reinstall
SciTools with Gnuplot as plotting engine:</p>
<div class="highlight-python"><pre>python setup.py install --easyviz_backend gnuplot</pre>
</div>
</div>
<div class="section" id="can-i-easily-turn-off-all-plotting">
<h3>Can I Easily Turn Off All Plotting?<a class="headerlink" href="#can-i-easily-turn-off-all-plotting" title="Permalink to this headline">¶</a></h3>
<p>Yes, this is very convenient when debugging other (non-plotting) parts
of a program. Just write:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scitools.std</span> <span class="kn">import</span> <span class="o">*</span>
<span class="n">turn_off_plotting</span><span class="p">(</span><span class="nb">globals</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="check-your-backends">
<h3>Check Your Backends!<a class="headerlink" href="#check-your-backends" title="Permalink to this headline">¶</a></h3>
<p>When you encounter a problem with Easyviz plotting, make sure that the
backend works correctly on its own (there may, e.g., be installation
problems with the backend - Easyviz just calls the backend to do the
plotting).</p>
<div class="section" id="gnuplot">
<h4>Gnuplot<a class="headerlink" href="#gnuplot" title="Permalink to this headline">¶</a></h4>
<p>For the Gnuplot backend you can try the following commands in a
terminal window:</p>
<div class="highlight-python"><pre>Unix/DOS&gt; gnuplot
gnuplot&gt; plot sin(x)</pre>
</div>
<p>This should result in a plot of the sine function on the screen.
If this command does not work, Easyviz will not work with the Gnuplot
backend. A common problem is that Gnuplot is installed, but the path
to the Gnuplot executable is not registered in the PATH environment
variable. See the section <em>Installing Gnuplot</em> if you need help with 
installing the Gnuplot program and its Python interface.</p>
</div>
<div class="section" id="matplotlib">
<h4>Matplotlib<a class="headerlink" href="#matplotlib" title="Permalink to this headline">¶</a></h4>
<p>The following code tests if you have installed Matplotlib correctly:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="mi">101</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<p>In case of problems, go to the Matplotlib source directory, remove the
build subdirectory, and try a new install with python setup.py install.</p>
</div>
</div>
<div class="section" id="trouble-with-gnuplot-and-threads">
<h3>Trouble with Gnuplot and Threads<a class="headerlink" href="#trouble-with-gnuplot-and-threads" title="Permalink to this headline">¶</a></h3>
<p>When using the Gnuplot backend, the following error may be encountered:</p>
<div class="highlight-python"><pre>thread.error: can't start new thread</pre>
</div>
<p>A remedy is to create fewer plots, and for animations, update the plot
window less frequently. For example:</p>
<div class="highlight-python"><pre>for i in range(number_of_frames_in_animation):
    &lt;prepare data&gt;
    if i % == 100:     # plot every 100 frames
        &lt;make plot&gt;</pre>
</div>
</div>
<div class="section" id="trouble-with-movie-making">
<h3>Trouble with Movie Making<a class="headerlink" href="#trouble-with-movie-making" title="Permalink to this headline">¶</a></h3>
<p>The call to movie demands that you have video encoders installed.
The legal encoders are mencoder, ffmpeg, mpeg_encode, ppmtompeg,
mpeg2enc, and convert. Some of these also require additional
software to be installed.</p>
<p>To install (e.g.) convert, you need to install the ImageMagick
software suite, since convert is a part of that package. ImageMagick
is easy to install on most platforms. The ppmtompeg encoder is a part
of the Netpbm software, while mpeg2enc is a part of mjpegtools.</p>
<p>On Linux Ubuntu you can issue the following installation command to install most of the available encoders for the movie function:</p>
<div class="highlight-python"><pre>Unix&gt; sudo apt-get install mencoder ffmpeg libavcodec-unstripped-51 netpbm mjpegtools imagemagick</pre>
</div>
<p>When something goes wrong with the movie making, check the output in
the terminal window. By default, Easyviz prints the command that makes
the movie. You can manually copy this command and run it again to start
finding out what can be wrong. Just switching to a different encoder can be
a quick remedy. The switch is done with the encoder keyword argument
to movie, e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Make animated GIF movie in the file tmpmovie.gif</span>
<span class="n">movie</span><span class="p">(</span><span class="s">&#39;tmp_*.png&#39;</span><span class="p">,</span> <span class="n">encoder</span><span class="o">=</span><span class="s">&#39;convert&#39;</span><span class="p">,</span> <span class="n">fps</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
      <span class="n">output_file</span><span class="o">=</span><span class="s">&#39;tmpmovie.gif&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="i-get-thread-errors-with-gnuplot">
<h3>I Get Thread Errors with Gnuplot<a class="headerlink" href="#i-get-thread-errors-with-gnuplot" title="Permalink to this headline">¶</a></h3>
<p>When plotting inside a loop, e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">some_values</span><span class="p">:</span>
    <span class="o">...</span>
    <span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">X0</span><span class="p">,</span> <span class="s">&#39;r-6&#39;</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> 
         <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;Xt&#39;</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s">&#39;My Title&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Gnuplot may lead to thread errors. A remedy is to do some plotting
outside the loop and then only update the data inside the loop:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">X0</span><span class="p">,</span> <span class="s">&#39;r-6&#39;</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> 
     <span class="n">xlabel</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">ylabel</span><span class="o">=</span><span class="s">&#39;Xt&#39;</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s">&#39;My Title&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">some_values</span><span class="p">:</span>
    <span class="o">...</span>
    <span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">X0</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="where-can-i-find-easyviz-documentation">
<h3>Where Can I Find Easyviz Documentation?<a class="headerlink" href="#where-can-i-find-easyviz-documentation" title="Permalink to this headline">¶</a></h3>
<p>There is a verbose Easyviz documentation that mainly focuses on an
introduction to Easyviz (what you read now is a part of that
documentation).</p>
<p>Another useful source of information is the many examples that come
with the SciTools/Easyviz source code. The examples are located in
the examples subfolder of the source.</p>
</div>
<div class="section" id="i-cannot-find-out-how-my-plot-can-be-created">
<h3>I Cannot Find Out How My Plot Can Be Created<a class="headerlink" href="#i-cannot-find-out-how-my-plot-can-be-created" title="Permalink to this headline">¶</a></h3>
<p>Note that Easyviz only support the most basic types of plots:</p>
<blockquote>
<div><ul class="simple">
<li>y=f(x) curves</li>
<li>bar plots</li>
<li>contour plots of 2D scalar fields</li>
<li>elevated 3D surfaces of 2D scalar fields</li>
<li>3D isosurfaces of 3D scalar fields</li>
<li>arrows reflecting 2D/3D vector fields</li>
<li>streamlines, streamtubes, and streamribbon for 3D vector fields.</li>
</ul>
</div></blockquote>
<p>For such standard plots you can use Easyviz, otherwise you have to
use a plotting package like Matplotlib, Gnuplot, or VTK directly
from your Python program.</p>
<p>The following Matlab-like commands (functions) are available (but not
supported by all backends):</p>
<blockquote>
<div><ul class="simple">
<li>autumn,</li>
<li>axes,</li>
<li>axis,</li>
<li>bone,</li>
<li>box,</li>
<li>brighten,</li>
<li>camdolly,</li>
<li>camlight,</li>
<li>camlookat,</li>
<li>campos,</li>
<li>camproj,</li>
<li>camroll,</li>
<li>camtarget,</li>
<li>camup,</li>
<li>camva,</li>
<li>camzoom,</li>
<li>caxis,</li>
<li>cla,</li>
<li>clabel,</li>
<li>clf,</li>
<li>close,</li>
<li>closefig,</li>
<li>closefigs,</li>
<li>colorbar,</li>
<li>colorcube,</li>
<li>colormap,</li>
<li>coneplot,</li>
<li>contour,</li>
<li>contour3,</li>
<li>contourf,</li>
<li>contourslice,</li>
<li>cool,</li>
<li>copper,</li>
<li>daspect,</li>
<li>figure,</li>
<li>fill,</li>
<li>fill3,</li>
<li>flag,</li>
<li>gca,</li>
<li>gcf,</li>
<li>get,</li>
<li>gray,</li>
<li>grid,</li>
<li>hardcopy,</li>
<li>hidden,</li>
<li>hold,</li>
<li>hot,</li>
<li>hsv,</li>
<li>ishold,</li>
<li>isocaps,</li>
<li>isosurface,</li>
<li>jet,</li>
<li>legend,</li>
<li>light,</li>
<li>lines,</li>
<li>loglog,</li>
<li>material,</li>
<li>mesh,</li>
<li>meshc,</li>
<li>openfig,</li>
<li>pcolor,</li>
<li>pink,</li>
<li>plot,</li>
<li>plot3,</li>
<li>prism,</li>
<li>quiver,</li>
<li>quiver3,</li>
<li>reducevolum,</li>
<li>savefig,</li>
<li>semilogx,</li>
<li>semilogy,</li>
<li>set,</li>
<li>shading,</li>
<li>show,</li>
<li><a href="#id17"><span class="problematic" id="id18">slice_</span></a>,</li>
<li>spring,</li>
<li>streamline,</li>
<li>streamribbon,</li>
<li>streamslice,</li>
<li>streamtube,</li>
<li>subplot,</li>
<li>subvolume,</li>
<li>summer,</li>
<li>surf,</li>
<li>surfc,</li>
<li>surfl,</li>
<li>title,</li>
<li>vga,</li>
<li>view,</li>
<li>white,</li>
<li>winter,</li>
<li>xlabel,</li>
<li>ylabel,</li>
<li>zlabel</li>
</ul>
</div></blockquote>
<p>Import statements in this module:
from scitools.globaldata import backend, VERBOSE
zlabel
from utils import *
from movie import movie</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/scitools_logo.jpg" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#"><tt class="docutils literal"><span class="pre">scitools.easyviz</span></tt></a><ul>
<li><a class="reference internal" href="#easyviz">Easyviz</a><ul>
<li><a class="reference internal" href="#easyviz-documentation">Easyviz Documentation</a></li>
<li><a class="reference internal" href="#guiding-principles">Guiding Principles</a></li>
</ul>
</li>
<li><a class="reference internal" href="#tutorial">Tutorial</a><ul>
<li><a class="reference internal" href="#a-note-on-import-statements">A Note on Import Statements</a></li>
<li><a class="reference internal" href="#plotting-a-single-curve">Plotting a Single Curve</a></li>
<li><a class="reference internal" href="#decorating-the-plot">Decorating the Plot</a></li>
<li><a class="reference internal" href="#plotting-multiple-curves">Plotting Multiple Curves</a></li>
<li><a class="reference internal" href="#making-multiple-figures">Making Multiple Figures</a></li>
<li><a class="reference internal" href="#controlling-line-styles">Controlling Line Styles</a></li>
<li><a class="reference internal" href="#interactive-plotting-sessions">Interactive Plotting Sessions</a></li>
<li><a class="reference internal" href="#making-animations">Making Animations</a></li>
<li><a class="reference internal" href="#moving-plot-window">Moving Plot Window</a></li>
<li><a class="reference internal" href="#advanced-easyviz-topics">Advanced Easyviz Topics</a><ul>
<li><a class="reference internal" href="#controlling-the-backend">Controlling the Backend</a></li>
<li><a class="reference internal" href="#importing-just-easyviz">Importing Just Easyviz</a></li>
<li><a class="reference internal" href="#setting-parameters-in-the-configuration-file">Setting Parameters in the Configuration File</a></li>
<li><a class="reference internal" href="#working-with-the-plotting-program-directly">Working with the Plotting Program Directly</a></li>
<li><a class="reference internal" href="#working-with-axis-and-figure-objects">Working with Axis and Figure Objects</a></li>
<li><a class="reference internal" href="#turning-off-all-plotting">Turning Off All Plotting</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#visualization-of-scalar-fields">Visualization of Scalar Fields</a><ul>
<li><a class="reference internal" href="#elevated-surface-plots">Elevated Surface Plots</a></li>
<li><a class="reference internal" href="#contour-plots">Contour Plots</a></li>
<li><a class="reference internal" href="#pseudocolor-plots">Pseudocolor Plots</a></li>
<li><a class="reference internal" href="#isosurface-plots">Isosurface Plots</a></li>
<li><a class="reference internal" href="#volumetric-slice-plot">Volumetric Slice Plot</a></li>
</ul>
</li>
<li><a class="reference internal" href="#visualization-of-vector-fields">Visualization of Vector Fields</a><ul>
<li><a class="reference internal" href="#quiver-plots">Quiver Plots</a></li>
<li><a class="reference internal" href="#stream-plots">Stream Plots</a></li>
<li><a class="reference internal" href="#bar-charts">Bar Charts</a></li>
</ul>
</li>
<li><a class="reference internal" href="#backends">Backends</a></li>
<li><a class="reference internal" href="#design">Design</a><ul>
<li><a class="reference internal" href="#main-objects">Main Objects</a></li>
</ul>
</li>
<li><a class="reference internal" href="#installation">Installation</a><ul>
<li><a class="reference internal" href="#installing-gnuplot">Installing Gnuplot</a></li>
<li><a class="reference internal" href="#linux-unix">Linux/Unix</a></li>
<li><a class="reference internal" href="#windows">Windows</a></li>
<li><a class="reference internal" href="#installing-matplotlib">Installing Matplotlib</a></li>
</ul>
</li>
<li><a class="reference internal" href="#troubleshooting">Troubleshooting</a><ul>
<li><a class="reference internal" href="#can-i-perform-a-diagnostic-test-of-easyviz">Can I Perform a Diagnostic Test of Easyviz?</a></li>
<li><a class="reference internal" href="#the-plot-window-disappears-immediately">The Plot Window Disappears Immediately</a></li>
<li><a class="reference internal" href="#i-get-thread-errors-while-plotting">I Get Thread Errors While Plotting</a></li>
<li><a class="reference internal" href="#i-get-strange-errors-saying-something-about-latex">I Get Strange Errors Saying Something About LaTeX</a></li>
<li><a class="reference internal" href="#old-programs-with-2d-scalar-vector-field-plotting-do-not-work">Old Programs with 2D Scalar/Vector Field Plotting Do Not Work</a></li>
<li><a class="reference internal" href="#can-i-easily-turn-off-all-plotting">Can I Easily Turn Off All Plotting?</a></li>
<li><a class="reference internal" href="#check-your-backends">Check Your Backends!</a><ul>
<li><a class="reference internal" href="#gnuplot">Gnuplot</a></li>
<li><a class="reference internal" href="#matplotlib">Matplotlib</a></li>
</ul>
</li>
<li><a class="reference internal" href="#trouble-with-gnuplot-and-threads">Trouble with Gnuplot and Threads</a></li>
<li><a class="reference internal" href="#trouble-with-movie-making">Trouble with Movie Making</a></li>
<li><a class="reference internal" href="#i-get-thread-errors-with-gnuplot">I Get Thread Errors with Gnuplot</a></li>
<li><a class="reference internal" href="#where-can-i-find-easyviz-documentation">Where Can I Find Easyviz Documentation?</a></li>
<li><a class="reference internal" href="#i-cannot-find-out-how-my-plot-can-be-created">I Cannot Find Out How My Plot Can Be Created</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="std.html"
                        title="previous chapter"><tt class="docutils literal"><span class="pre">scitools.std</span></tt></a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="blt_.html"
                        title="next chapter"><tt class="docutils literal docutils literal"><span class="pre">scitools.easyviz.blt_</span></tt></a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/easyviz.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="blt_.html" title="scitools.easyviz.blt_"
             >next</a> |</li>
        <li class="right" >
          <a href="std.html" title="scitools.std"
             >previous</a> |</li>
        <li><a href="index.html">SciTools 0.8.3 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, H. P. Langtangen, J. Ring, ++.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
    </div>
  </body>
</html>